2013-09-01 23 views
0

我試圖運行測試程序來證明Apache Derby的安裝正常。
安裝已給予本教程Install Apache Derby on Ubuntu使用Apache Derby運行TestDB程序--java.sql.SQLException:url不能爲空

對於運行程序具有從終端輸入:

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:

​​

ntent對於數據庫的用戶名數據庫密碼空行。

並運行後輸出:

[email protected]:~/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) 

更新:

我嘗試使用Derby嵌入式驅動程序。我改變database.properties

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

但運行後,我有一個輸出:

[email protected]:~/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

'jdbc:oracle:thin:@ larry.mathcs.sjsu.edu:1521:InvoiceDB'不是Derby的有效連接URL –

回答

3

屬性文件的形式

key1=value1 
key2=value2 

你忘了鍵,以便所有的props.getProperty()調用將返回空的。

其次,您的數據庫URL與德比數據庫不匹配。它看起來更像是一個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,URL將爲jdbc:derby:InvoiceDB;create=true;user=me;password=mine

+0

我在嘗試運行後更新了問題。把這個測試改成在Apache服務器上使用mysql DB怎麼樣?我應該使用哪個驅動程序?使用MyPHPAdmin。 –

+1

您需要在類路徑中包含'derby.jar'。看看這裏:http://db.apache。org/derby/papers/DerbyTut/install_software.html#derby_configure – vanje

+0

當然你可以切換到MySQL。然後,您需要相應的驅動程序JAR,將其包含在您的類路徑中並更改驅動程序類。使用MyPHPAdmin,您可以管理您的MySQL數據庫,但這與從Java訪問數據庫無關。 (您可以使用像[松鼠](http://squirrel-sql.sourceforge.net/)這樣的工具來管理所有類型的數據庫。) – vanje