使用Apache Derby运行TestDB程序 - java.sql.SQLException:url不能为null

时间:2013-09-01 11:43:12

标签: java sql exception derby

我尝试运行测试程序来证明Apache Derby的安装没问题 安装符合本教程Install Apache Derby on Ubuntu

对于run程序,必须从终端输入:

java -classpath driver_class_path:. TestDB database.properties

来自TestDB class:的代码

public class TestDB 
{
   public static void main(String[] args) throws Exception
   {   
      if (args.length == 0)
      {   
         System.out.println(
               "Usage: java -classpath driver_class_path" 
               + File.pathSeparator 
               + ". TestDB database.properties");
         return;
      }
      else 
         SimpleDataSource.init(args[0]);

      Connection conn = SimpleDataSource.getConnection();
      try
      {
         Statement stat = conn.createStatement();

         stat.execute("CREATE TABLE Test (Name CHAR(20))");
         stat.execute("INSERT INTO Test VALUES ('Romeo')");

         ResultSet result = stat.executeQuery("SELECT * FROM Test");
         result.next();
         System.out.println(result.getString("Name"));

         stat.execute("DROP TABLE Test");
      }
      finally
      {
         conn.close();
      }
   }
}

来自SimpleDataSource class:

public class SimpleDataSource
{
   private static String url;
   private static String username;
   private static String password;

   /**
      Initializes the data source.
      @param fileName the name of the property file that 
      contains the database driver, URL, username, and password
   */
   public static void init(String fileName)
         throws IOException, ClassNotFoundException
   {  
      Properties props = new Properties();
      FileInputStream in = new FileInputStream(fileName);
      props.load(in);

      String driver = props.getProperty("jdbc.driver");
      url = props.getProperty("jdbc.url");
      username = props.getProperty("jdbc.username");
      if (username == null) username = "";
      password = props.getProperty("jdbc.password");
      if (password == null) password = "";
      if (driver != null)
         Class.forName(driver);
   }

   /**
      Gets a connection to the database.
      @return the database connection
   */
   public static Connection getConnection() throws SQLException
   {
      return DriverManager.getConnection(url, username, password);
   }
}

database.properties:

的内容
jdbc:oracle:thin:@larry.mathcs.sjsu.edu:1521:InvoiceDB
/usr/share/javadb

数据库用户名 数据库密码 空行。

运行后输出:

nazar_art@nazar-desctop:~/Desktop/Big JAVA/bj4_code/ch22/test$ java -classpath /usr/share/javadb:. TestDB database.properties
Exception in thread "main" java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(DriverManager.java:556)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at SimpleDataSource.getConnection(SimpleDataSource.java:45)
    at TestDB.main(TestDB.java:26)

更新

我尝试使用德比嵌入式驱动程序。我将database.properties更改为

jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
jdbc.url=jdbc:derby:InvoiceDB;create=true;
user=me;
password=mine 

但是在跑完之后我有下一个输出:

nazar_art@nazar-desctop:~/Desktop/Big JAVA/bj4_code/ch22/test$ java -classpath /usr/share/javadb:. TestDB database.properties
Exception in thread "main" java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at SimpleDataSource.init(SimpleDataSource.java:36)
    at TestDB.main(TestDB.java:24)

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

属性文件的格式为

key1=value1
key2=value2

您忘记了密钥,因此所有props.getProperty()次调用都将返回null。

其次,您的数据库URL与derby数据库不匹配。它看起来更像是Oracle数据库。你使用什么驱动程序类?

也许这会起作用

jdbc.driver=org.apache.derby.jdbc.ClientDriver
jdbc.url=jdbc:derby://localhost:1527/InvoiceDB;create=true;user=me;password=mine

请务必事先启动Derby服务器。

如果您想使用嵌入式驱动程序,则驱动程序类为org.apache.derby.jdbc.EmbeddedDriver,网址为jdbc:derby:InvoiceDB;create=true;user=me;password=mine