Java 和 MySQL - "Unrecognized statement"


package main.java;
import java.sql.*;
public class SQLSetup {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/test_db";
//  Database credentials
static final String USER = "Halli";
static final String PASS = "dragon";
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 REGISTRATION " +
"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
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!");
}
}

为什么CREATE TABLE REGISTRATION会给我错误"无法识别的语句"?我使用的是Intellij、Java 13、Maven和MySQL服务器。

这只是为了解决我的问题,因为模板抱怨我没有提供足够的细节和大量的代码,但我不确定该怎么说。

我甚至没有想到我可以在Intellij给我一个红色错误的情况下运行代码,但这并不重要-我尝试运行代码,Voila-它创建了一个表,即使有这个错误消息。

相关内容

最新更新