我有一個語法SQL的問題。當我mvn jetty:run
然後會出現以下錯誤:錯誤:java.sql.SQLSyntaxErrorException,該如何解決?
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "user" at line 1, co lumn 14. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknow n 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(Un known Source) at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown So urce) at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown So urce) at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) at edu.java.spring.service.user.controller.CustomContextLoaderListener.c reateTableNotExist(CustomContextLoaderListener.java:68) at edu.java.spring.service.user.controller.CustomContextLoaderListener.c reateTable(CustomContextLoaderListener.java:48) at edu.java.spring.service.user.controller.CustomContextLoaderListener.c ontextInitialized(CustomContextLoaderListener.java:36)
這裏提交CustomContextLoaderListene
其中encouter錯誤:
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();
}
}
謝謝。 –