troubleshooting Question

JDBC Problem: Schema '...' does not exist

Avatar of UrosVidojevic
UrosVidojevicFlag for Serbia asked on
Java
16 Comments1 Solution3595 ViewsLast Modified:
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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 16 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros