简体   繁体   English

Java:在数据库中创建表

[英]Java : Create Table in Database

I have been trying to create a table in the database. 我一直在尝试在数据库中创建表。 The code shows no error and runs fine but in the end no table was created in the database. 该代码未显示任何错误且运行正常,但最后在数据库中未创建任何表。 Help please. 请帮助。

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());
    }
}

This should do it 这应该做

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 );

Dont forget to put the derbyclient.jar in you classpath and close the connection 不要忘记将derbyclient.jar放在类路径中并关闭连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM