Java:在数据库中创建表

时间:2014-09-05 14:36:01

标签: java mysql

我一直在尝试在数据库中创建一个表。代码显示没有错误并且运行正常,但最终没有在数据库中创建表。请帮助。

public void createTable(){

    try {

    String host = "jdbc:derby://localhost:1537/fypDB";
    String uname = "test";
    String pword = "test";
    Connection con = DriverManager.getConnection( host, uname, pword );
    Statement stmt = con.createStatement();

   //username is valid variable from main class 
    String SQL = "CREATE TABLE "+username+" (classId VARCHAR(255), "+"PRIMARY KEY ( classId ))";
    stmt.executeUpdate( SQL );
    System.out.println(SQL);

    } catch (SQLException err){
        System.out.println(err.getMessage());
    }
}

1 个答案:

答案 0 :(得分:0)

这应该这样做

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1537/fypDB", 
                                                  "yourUsername", "ypurPassword");
Statement stmt = con.createStatement();
stmt.execute( SQL );

别忘了将derbyclient.jar放在类路径中并关闭连接