0

I am trying to create a table using java for mysql. The code is below.

  Statement stmt2 = con.createStatement();

  String sql = "CREATE TABLE " + libcard +
               "(Title INTEGER, " +
               " Director VARCHAR(255), " + 
               " dateBorrowed DATE, " + 
               " dueDate DATE)"; 

  stmt.executeUpdate(sql);

but there is always error. :: Exception message is given below :_

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '23232.0(Title INTEGER, Director VARCHAR(255), dateBorrowed DATE, dueDate DATE' at line 1

6
  • 5
    What kind of table name is 23232.0? Commented Sep 28, 2013 at 13:15
  • @juergend: I'm guessing (hoping) it's a typo rather than a table name... Commented Sep 28, 2013 at 13:17
  • The error message is probably copied from the output. That is no typo IMO. Commented Sep 28, 2013 at 13:18
  • 1
    @eggyal: I'm not saying it's invalid or wrong, I'm saying it's meaningless. Commented Sep 28, 2013 at 13:21
  • 1
    @lzmaki if you don't know the domain, no way of saying it is meaningless. It could be a table holding data of object number 23232.0. Commented Sep 28, 2013 at 13:25

2 Answers 2

2

Check the documentation on how to come up with valid table names. In your case 23232.0, you should surround it with backticks:

String sql = "CREATE TABLE `" + libcard + "`" +
           " (Title INTEGER, " +
           " Director VARCHAR(255), " + 
           " dateBorrowed DATE, " + 
           " dueDate DATE)"; 
Sign up to request clarification or add additional context in comments.

Comments

0

try this code

try{

      Class.forName("com.mysql.jdbc.Driver");


      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");


      System.out.println("Creating table in given database...");
      stmt = conn.createStatement();

      String sql = "CREATE TABLE " + libcard +
               "(Title INT(2), " +
               " Director VARCHAR(255), " + 
               " dateBorrowed DATE, " + 
               " dueDate DATE)"; 

      stmt.executeUpdate(sql);
      System.out.println("Created table in given database...");
   }catch(SQLException se){

      se.printStackTrace();
   }catch(Exception e){

      e.printStackTrace();
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.