错误:java.sql.SQLSyntaxErrorException,如何解决?

时间:2016-03-21 14:52:33

标签: spring hibernate jetty

我的语法SQL有问题。当mvn jetty:run出现以下错误时:

  

java.sql.SQLSyntaxErrorException:语法错误:遇到“user”at   第1行,第14行。           at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknow   n来源)           在org.apache.derby.impl.jdbc.Util.generateCsSQLException(未知来源)   )           在org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException   (未知来源)           at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Un   已知来源)           at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown So   urce)           at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown So   urce)           在org.apache.derby.impl.jdbc.EmbedStatement.execute(未知来源)           在org.apache.derby.impl.jdbc.EmbedStatement.execute(未知来源)           在edu.java.spring.service.user.controller.CustomContextLoaderListener.c   reateTableNotExist(CustomContextLoaderListener.java:68)           在edu.java.spring.service.user.controller.CustomContextLoaderListener.c   reateTable(CustomContextLoaderListener.java:48)           在edu.java.spring.service.user.controller.CustomContextLoaderListener.c   ontextInitialized(CustomContextLoaderListener.java:36)

此处文件CustomContextLoaderListene包含错误:

package edu.java.spring.service.user.controller;

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.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoaderListener;

public class CustomContextLoaderListener extends ContextLoaderListener{

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // TODO Auto-generated method stub
        System.out.println("hibernate shutdown database");
        try {
            DriverManager.getConnection("jdbc:derby:D:/PROJECTSPRING/userdb;shutdown=true");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("\n Spring-MVC application destroyed \n");
        super.contextDestroyed(event);

    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // TODO Auto-generated method stub
        System.out.println("\n Spring-MVC application inited \n");
        try {
            createTable();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.contextInitialized(event);
    }

    public void createTable() throws SQLException{
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            Connection connection = DriverManager.getConnection("jdbc:derby:D:/PROJECTSPRING/subjectdb;create=true");
            createTableNotExist(connection,"user", "create table user"
                    + "(username varchar(1000) primary key,"
                    + "password varchar(1000),birthday date,"
                    + "age integer,gender varchar(100))");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public void createTableNotExist(Connection connection,
            String tableName,String createTableSQL) throws SQLException{
        DatabaseMetaData dbmd = connection.getMetaData();
        ResultSet rs = dbmd.getTables(null, null,tableName.toUpperCase(), null);
        if (rs.next()){
            System.out.println("Table" + rs.getString("TABLE_NAME") + "already exists");
            return;

        }
        Statement statement = connection.createStatement();
        statement.execute(createTableSQL);
        System.out.println("\n\n executed" + createTableSQL + "\n\n");
        statement.close();
    }


}

1 个答案:

答案 0 :(得分:0)

“user”它是德比中的内置函数。输入另一个表名,它应该开始工作