java - 无法找出 JDBC 错误

标签 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 似乎是正确的,但我想我遗漏了一些东西。

最佳答案

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))" ; 

关于java - 无法找出 JDBC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995394/

相关文章:

php - 查询失败时脚本不重定向

scala - 如何在 Scala 中使用 foreach 循环来改变对象?

oracle - 没有更多数据可从套接字错误中读取

javascript - 点击后如何加载特定片段?

Java 9 错误 : not in a module on the module source path

java - 使用 Java 代码的可用 J2ME SDK 有哪些?

PHP:扫描数据库中的某个值然后组织它

php - PHP 中的数组结果

java - RMI 中命名和注册表的区别

java.sql.SQLException : [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query