我正在尝试使用jdbc创建一个简单的表,但它给了我一个错误。这是我的代码:
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javabook","scott","tiger");
System.out.println("part 1:DONE !");
Statement statement = con.createStatement();
String query = "create table Enrollment (ssn char(9), courseId char(5), dateRegistered date, grade char(2)"+
"primary key (ssn, courseId), foreign key (ssn) references Student (ssn), foreign key (courseId) references Course(courseId)";
statement.executeUpdate(query);
System.out.println("part 2:DONE !");
}
}
我收到此错误:
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at Test.main(Test.java:17)
答案 0 :(得分:0)
你错过了右括号')'在你的代码中。在代码中使用以下语句。
字符串查询="创建表注册(ssn char(9),courseId char(5),dateRegistered date,grade char(2)" + "主键(ssn,courseId),外键(ssn)引用学生(ssn),外键(courseId)引用课程(courseId))&#34 ;;
请确保右括号。