Hi Java Experts,
I'm currently learning some JDBC basics and experimenting with Derby db. Everything was going fine, until I tried to create table from the following program:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Properties;
public class SQLExample03 {
final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
final String protocol = "jdbc:derby:";
final String dbname = "CarsDB";
public void work() {
try {
Class.forName(driver).newInstance();
Properties props = new Properties();
props.put("user", "sysdba");
props.put("password", "masterkey");
Connection c = DriverManager.getConnection(
protocol + dbname + ";create=true", props);
Statement s = c.createStatement();
s.execute("CREATE TABLE CARS(" +
" CarID CHAR(4) PRIMARY KEY, " +
" Name CHAR(20) NOT NULL DEFAULT 'Unknown', " +
" Cost INT NOT NULL CHECK(Cost BETWEEN 1000 AND 100000))");
PreparedStatement ps = c.prepareStatement(
"INSERT INTO CARS VALUES (?,?,?)");
ps.setString(1, "0001");
ps.setString(2, "Ferrari");
ps.setInt(3, 100000);
ps.execute();
ps.setString(1, "0002");
ps.setString(2, "Audi");
ps.setInt(3, 50000);
ps.execute();
ps.setString(1, "0003");
ps.setString(2, "Ford");
ps.setInt(3, 80000);
ps.execute();
ResultSet rs = s.executeQuery("SELECT * FROM Cars");
while (rs.next()) {
System.out.println(rs.getString(1) + " | " +
rs.getString(2) + " | " +
rs.getInt(3));
}
rs.close();
ps.close();
s.close();
c.close();
boolean gotSQLExc = false;
try {
DriverManager.getConnection(protocol + ";shutdown=true");
} catch (SQLException e) {
gotSQLExc = true;
}
if (!gotSQLExc) {
System.out.println("Database did not shut down normally");
} else {
System.out.println("Database shut down normally");
}
} catch (Throwable e) {
System.out.println("Exception thrown: ");
if (e instanceof SQLException) {
printSQLError((SQLException)e);
} else {
e.printStackTrace();
}
}
}
private static void printSQLError(SQLException e) {
while (e != null) {
System.out.println(e.toString());
e = e.getNextException();
}
}
public static void main(String[] args) {
new SQLExample03().work();
}
}
When I try to run it, i get this output.
Exception thrown:
java.sql.SQLSyntaxErrorException: Schema 'SYSDBA' does not exist
Very strange thing is that, when I comment lines in this program that create and update table CARS and replace them with for example:
s.execute("CREATE TABLE X(Y INT)");
Run program, and then:
* remove previous line
* uncomment previously commented lines
* run program again,
table CARS is created without any exceptions.
Also when I make simplier tables without key words DEFAULT, CHECK,... I don't get any exceptions.
I have tried for hours, to figure out what is the problem, but it simply doesn't make any sense to me.
Any ideas, what is wrong?
Our community of experts have been thoroughly vetted for their expertise and industry experience.