public void create(String name) {
create_name = name;
try {
getConnection();
q = "create table ? (emp_id number (10), emp_name varchar (15))";
pst = con.prepareStatement(q);
pst.setString(1, create_name);
int j = pst.executeUpdate();
JOptionPane.showMessageDialog(new JDialog(), "Table is Created");
} catch (Exception e) {
JOptionPane.showMessageDialog(new JDialog(), "Table is not Created");
}
}
Asked
Active
Viewed 58 times
-2
-
did you get any error, did your table created, what is your question exactly ? – Youcef LAIDANI Feb 23 '17 at 19:39
-
1You cannot parameterize object identifiers like table names – Alex K. Feb 23 '17 at 19:43
1 Answers
1
To create a table with PrepapredStatement
this is not possible, because when you make
q= "create table ? (emp_id number (10), emp_name varchar (15))";
So in reality it will execute this query here :
create table 'table_name' (emp_id number (10), emp_name varchar (15))
and the 'table_name'
is forbidden when you create a table, so instead to that i suggest to play with your PrepapredStatement
like this :
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(DB_URL, DB_username, DB_password);
PreparedStatement preparedStatement = connection.prepareStatement("create table ? (emp_id number (10), emp_name varchar (15))");
preparedStatement.setString(1, "table_name");
System.out.println(preparedStatement.toString());
Statement st = connection.createStatement();
int i = st.executeUpdate(preparedStatement.toString().replaceAll("\"", "\""));
JOptionPane.showMessageDialog(new JDialog(), "Table is Created");
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(new JDialog(), "Table is not Created");
}
The idea is simple when you prepare your Statement you will get the previous query you can get it like this preparedStatement.toString()
so you can change the ''
in the start and in the end with ""
because "
is allowed in oracle to create a table with a name which contain spaces.

Youcef LAIDANI
- 55,661
- 15
- 90
- 140