java.sql.SQLSyntaxErrorException:语法错误:在第1行第8列遇到“ TABLEBOOK”

时间:2019-05-30 06:48:43

标签: javafx derby

我正在按照您的教程制作图书馆管理系统,但是当我尝试在derby数据库中创建表时,会产生语法错误。

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "TABLEBOOK" at line 1, column 8.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at library.assistant.database.DatabaseHandler.setupBookTable(DatabaseHandler.java:48)
at library.assistant.database.DatabaseHandler.<init>(DatabaseHandler.java:24)
at library.assistant.ui.addbook.FXMLDocumentController.initialize(FXMLDocumentController.java:48)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at library.assistant.ui.addbook.LibraryAssistant.start(LibraryAssistant.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Caused by: ERROR 42X01: Syntax error: Encountered "TABLEBOOK" at line 1, column 8.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ParserImpl.parseStatementOrSearchCondition(Unknown Source)
at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 23 more

这是我的数据库处理程序代码:

package library.assistant.database;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;


public final class DatabaseHandler {

    private static DatabaseHandler handler;

    private static final String DB_URL = "jdbc:derby:database;create=true";
    private static Connection conn = null;
    private static Statement stmt = null;

public DatabaseHandler(){
    createConnection();
    setupBookTable();
} 

void createConnection(){
    try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        conn = DriverManager.getConnection(DB_URL);
    }catch(Exception e){
        e.printStackTrace();
    }
}

void setupBookTable(){
    String TABLE_NAME = "BOOK";
    try {
        stmt = conn.createStatement();

        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);

        if (tables.next()){
            System.out.println("Table " + TABLE_NAME + "already exists! ready for go!");
        }
        else {
            stmt.execute("CREATE TABLE" + TABLE_NAME + "("
                       + "     id varchar(200) primary key, \n"
                       + "     title varchar(200), \n"
                       + "     author varchar(200), \n"
                       + "     publisher varchar(200), \n"
                       + "     isAvail boolean default true"
                       + ")");
        }
    }catch(SQLException e){
        System.err.println(e.getMessage() + ".......setupDatabase");
    }finally{    
    }
}


public ResultSet execQuery(String query){
    ResultSet result;
    try{
        stmt = conn.createStatement();
        result = stmt.executeQuery(query);
    }catch(SQLException ex){
        System.out.println("Exception at execQuery: dataHandler" + ex.getLocalizedMessage());
        return null;
    }finally {

    }
    return result;
}

public boolean execAction (String qu){
    try{
        stmt = conn.createStatement();
        stmt.execute(qu);
        return true;
    }catch(SQLException ex){
        JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Error Occured", JOptionPane.ERROR_MESSAGE);
        System.out.println("Exception at execQuery: dataHandler" + ex.getLocalizedMessage());
        return false;
    }finally {

    }
}

} 我从YouTube教程中获得了此文件,但它似乎对我不起作用。 任何帮助将不胜感激。...

1 个答案:

答案 0 :(得分:-1)

void createConnection(){
    try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        conn=DriverManager.getConnection(BD_URL);
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

void setupEmployeeTable(){
    String TABLE_NAME = "EMPLOYEE";
    
    try{
        
        stmt = conn.createStatement();
        
        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);
        if (tables.next()){
            System.out.print("Table" + TABLE_NAME +" READY");
            
        }
        else{
            stmt.execute("CREATE TABLE Employee("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY"
                    +"(START WITH 1,INCREMENT BY 1),"
                    + "name varchar(200),"
                    + "phone varchar(200),"
                    + "time timestamp default CURRENT_TIMESTAMP)");
                   
        }
    }catch(SQLException e){
        System.err.println(e.getMessage()+"....setupdatabase ");
    }finally
            {
            }      
}