public static void main(String[] args) throws Exception {
// TODO code application logic here
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection c = DriverManager
.getConnection("jdbc:derby:DatabaseTrial;create=true");
System.out.println("connection ok");
String schema = "CREATE SCHEMA ABC";
String subj = "CREATE TABLE ABC.SUBJ (SUBJ_NO INT NOT NULL, DESCRIPTIVE_TITLE VARCHAR(80) NOT NULL, TEACHER VARCHAR(80), PRIMARY KEY (SUBJ_NO))";
String table = "CREATE TABLE ABC.REQ (REQ_NO INT NOT NULL, " +
"STATUS CHAR(8) DEFAULT NULL, SUBJ_NO INT NOT NULL, PRIMARY KEY (REQ_NO), FOREIGN KEY (SUBJ_NO) REFERENCES ABC.SUBJ(SUBJ_NO) ON DELETE CASCADE ON UPDATE CASCADE)";
String insert = "INSERT INTO ABC.REQ VALUES (1, DEFAULT, 1)";
String insert2 = "INSERT INTO ABC.SUBJ VALUES (1, 'Programming Application Lab', 'Mr. CBO Montes')";
Statement s = c.createStatement();
s.executeUpdate(schema);
s.executeUpdate(subj);
s.executeUpdate(table);
s.executeUpdate(insert2);
s.executeUpdate(insert);
ResultSet rs = s.executeQuery("SELECT * FROM ABC.MINE NATURAL JOIN ABC.SUBJ");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString("STATUS") + " " + rs.getString("Subj_no") + " " + rs.getString("DESCRIPTIVE_TITLE") + " " + rs.getString("TEACHER"));
}
c.close();
}
我总是收到以下错误消息:
在 "main" 线程中的异常 java.sql.SQLSyntaxErrorException: 语法错误: 在第1行,第196列遇到"CASCADE"。 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.executeLargeUpdate(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.executeUpdate(Unknown Source) at TrialDBEmbedded.main(TrialDBEmbedded.java:26) 由于以下原因造成:ERROR 42X01: 在第1行,第196列遇到语法错误:"CASCADE"。 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) ... 4 more