Java 和 MySQL – “无法识别的语句”

huangapple 未分类评论37阅读模式
英文:

Java and 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" 出现 "Unrecognized statement" 错误的原因是什么?我正在使用IntelliJ、Java 13、Maven和MySQL服务器。

这只是为了让我的问题通过模板,因为模板在抱怨我没有提供足够的细节和大量的代码,但我不确定还有什么更多关于这个问题要说的。

英文:
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!");
   }
}

Why is CREATE TABLE REGISTRATION giving me the error "Unrecognized statement"? I am using Intellij, Java 13 and Maven and MySQL server.

This is just something to get my question through since the template is complaining about me not giving enough details and a lot of code, but I am not sure what more to say about the problem.

答案1

得分: 0

我甚至没有想到我可以使用Intellij运行代码,它在这个地方给了我一个红色错误,但这并不重要——我尝试运行代码,哇——它创建了一个表,即使有这个错误消息。

英文:

It did not even occur to me that I could run the code with Intellij giving me a red error on this, but it did not matter - I tried to run the code and Voila - it created a table, even with this error message.

huangapple
  • 本文由 发表于 2020年4月5日 02:04:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61032498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定