我正在尝试使用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服务器版本对应的手册,以便在'23232.0(标题INTEGER,Director VARCHAR(255),dateBorrowed DATE,dueDate DATE'第1行附近使用正确的语法
答案 0 :(得分:2)
检查documentation如何提供有效的表名。在你的情况23232.0
中,你应该用反引号包围它:
String sql = "CREATE TABLE `" + libcard + "`" +
" (Title INTEGER, " +
" Director VARCHAR(255), " +
" dateBorrowed DATE, " +
" dueDate DATE)";
答案 1 :(得分: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();
}