0

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());
    }
}
User420
  • 137
  • 3
  • 12
  • 3
    Is auto-commit enabled? – Elliott Frisch Sep 05 '14 at 14:36
  • you shouldn't be creating a table for every user. have ONE table and make username a field within that table. – Marc B Sep 05 '14 at 14:40
  • According to this http://stackoverflow.com/questions/11962028/create-table-in-by-sql-statement-using-executeupdate-in-mysql you're missing a semi-colon: `String SQL = "CREATE TABLE "+username+" (classId VARCHAR(255), "+"PRIMARY KEY ( classId ));";` – StephaneM Sep 05 '14 at 14:44
  • Print your SQL on the terminal and try to run it directly on mysql to see what happens. – AndreDuarte Sep 05 '14 at 14:51

1 Answers1

0

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

SparkOn
  • 8,806
  • 4
  • 29
  • 34