0

我正在尝试使用 java for mysql 创建一个表。代码如下。

  Statement stmt2 = con.createStatement();

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

  stmt.executeUpdate(sql);

但总是有错误。:: 异常消息如下:_

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '23232.0(Title INTEGER, Director VARCHAR(255), dateBorrowed DATE, dueDate DATE' 附近使用正确的语法

4

2 回答 2

2

查看有关如何提出有效表名的文档。在你的情况下23232.0,你应该用反引号括起来:

String sql = "CREATE TABLE `" + libcard + "`" +
           " (Title INTEGER, " +
           " Director VARCHAR(255), " + 
           " dateBorrowed DATE, " + 
           " dueDate DATE)"; 
于 2013-09-28T13:22:31.923 回答
0

试试这个代码

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();
   }
于 2013-09-28T13:19:04.543 回答