Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc_test;
- import java.sql.*;
- import java.util.Scanner;
- public class JDBC_Test {
- public static void main(String[] args) {
- String tableName=null;
- Scanner input = new Scanner(System.in);
- int id=8;
- System.out.println("Enter the table name: ");
- tableName = input.nextLine();
- try
- {
- Connection con = DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:oracle12c", "system", "oracle12c");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from "+tableName+"where id='"+id+"'");
- System.out.printf("%15s%15s","Name","ID");
- while (rs.next())
- {
- System.out.printf("n%15s",rs.getString("name"));
- }
- con.close();
- stmt.close();
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
- Enter the table name:
- t
- java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
- try
- {
- Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle12c", username, password);
- // You want to prepare a statement so you can pass parameters.
- PreparedStatement stmt = con.prepareStatement();
- // The first question mark (?)
- // Assuming this is an INT type. If not, there is setString, setLong, etc.
- ps.setInt(1, id);
- // Note the first question mark is where the ID is supposed to be. This is a parameter.
- ResultSet rs = stmt.executeQuery("select * from tableName where id = ?");
- while (rs.next())
- {
- // Replace "column_name" with the name of your column
- System.out.println(rs.getString("column_name"));
- }
- con.close();
- stmt.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement