2017-02-23 38 views
-2
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"); 
    } 

} 
+0

你有没有得到任何错误,你的表格创建了,你的问题到底是什么? –

+1

您无法参数化对象标识符,如表名称 –

回答

1

要创建一个表PrepapredStatement这是不可能的,因为当你做

q= "create table ? (emp_id number (10), emp_name varchar (15))"; 

因此,在现实中,它会执行这个查询这里:

create table 'table_name' (emp_id number (10), emp_name varchar (15)) 

'table_name'在您创建表格时被禁止,因此建议您使用PrepapredStatement这样玩:

当您准备报表,你会得到前面的查询就可以得到像这样 preparedStatement.toString()这样你就可以改变的开始,并与 ""到底 ''因为 "被允许在甲骨文
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"); 
} 

的想法很简单create a table with a name which contain spaces