无法弄清楚JDBC的错误

时间:2018-02-26 19:06:21

标签: java mysql jdbc

我正在创建一个为我的编码类创建数据库的项目。但是,当我运行此代码时

import java.sql.*;

public class addTable {
// JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://LocalHost/internData";

 //  Database credentials
static final String USER = "root";
static final String PASS = "password";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
  //STEP 2: Register JDBC driver
  Class.forName("com.mysql.jdbc.Driver");

  //STEP 3: Open a connection
  System.out.println("Connecting to a selected database...");
  conn = DriverManager.getConnection(DB_URL, USER, PASS);
  System.out.println("Connected database successfully...");

  //STEP 4: Execute a query
  System.out.println("Creating table in given database...");
  stmt = conn.createStatement();

  String sql = "CREATE TABLE TREFYY " +
               "(EntryNumber INTEGER not NULL, " +
               " Date VARCHAR(255), " + 
               " CriterionNum INTEGER not NULL, " + 
               " Score VARCHAR(255) " ; 

  stmt.executeUpdate(sql);
  System.out.println("Created table in given database...");
}catch(SQLException se){
  //Handle errors for JDBC
  se.printStackTrace();
 }catch(Exception e){
  //Handle errors for Class.forName
  e.printStackTrace();
}finally{
  //finally block used to close resources
  try{
     if(stmt!=null)
        conn.close();
   }catch(SQLException se){
   }// do nothing
   try{
     if(conn!=null)
        conn.close();
   }catch(SQLException se){
     se.printStackTrace();
  }//end finally try
}//end try
System.out.println("Goodbye!");
}

我收到此错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '' at line 1

如何解决此错误?在所有指南中,我看起来SQL看似正确,但我想我错过了一些东西。

3 个答案:

答案 0 :(得分:0)

你错过了')'在SQL的末尾。

...得分VARCHAR(255))

答案 1 :(得分:0)

String sql = "CREATE TABLE TREFYY " +
               "(EntryNumber INTEGER not NULL, " +
               " Date VARCHAR(255), " + 
               " CriterionNum INTEGER not NULL, " + 
               " Score VARCHAR(255) " ; 

您在sql语句结束时错过了结束 )

String sql = "CREATE TABLE TREFYY " +
                   "(EntryNumber INTEGER not NULL, " +
                   " Date VARCHAR(255), " + 
                   " CriterionNum INTEGER not NULL, " + 
                   " Score VARCHAR(255))" ; 

答案 2 :(得分:0)

您错过了类型后最后一行的结束括号。

String sql = "CREATE TABLE TREFYY " +
               "(EntryNumber INTEGER not NULL, " +
               " Date VARCHAR(255), " + 
               " CriterionNum INTEGER not NULL, " + 
               " Score VARCHAR(255)) " ;