使用apachederby-java.sql.SQLException运行TestDB程序:url不能为null
我试图运行测试程序来证明ApacheDerby的安装是正常的。使用apachederby-java.sql.SQLException运行TestDB程序:url不能为null,java,sql,exception,derby,Java,Sql,Exception,Derby,我试图运行测试程序来证明ApacheDerby的安装是正常的。 安装本教程 对于运行程序,必须从终端键入: java -classpath driver_class_path:. TestDB database.properties 来自TestDB类的代码: public class TestDB { public static void main(String[] args) throws Exception { if (args.length == 0)
安装本教程 对于运行程序,必须从终端键入:
java -classpath driver_class_path:. TestDB database.properties
来自TestDB类的代码:
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();
}
}
}
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);
}
}
从SimpleDataSource类:
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();
}
}
}
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);
}
}
数据库的内容。属性:
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)
更新:
我尝试使用derby嵌入式驱动程序。我将数据库.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)
如何解决此问题?属性文件的格式为
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
,URL是jdbc:derby:InvoiceDB;create=true;用户=我;password=mine
属性文件的格式如下
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
,URL是jdbc:derby:InvoiceDB;create=true;用户=我;password=mine
jdbc:oracle:thin:@larry.mathcs.sjsu.edu:1521:InvoiceDB不是Derby的有效连接URLjdbc:oracle:thin:@larry.mathcs.sjsu.edu:1521:InvoiceDB
不是DerbyI尝试运行后更新的问题的有效连接URL。把这个测试改为在Apache服务器上使用mysql DB怎么样。我应该用哪个司机?使用MyPHPAdmin。您需要将derby.jar
包含到类路径中。看看这里:当然你可以切换到MySQL。然后需要适当的驱动程序JAR,将其包含在类路径中并更改驱动程序类。使用MyPHPAdmin,您可以管理MySQL数据库,但这与从Java访问数据库无关。(您可以使用类似于管理各种不同数据库的工具)好的,但这与Java类路径无关。在运行应用程序的命令中,除了您的问题之外,还包括您的derby.jar
(将您的占位符driver\u class\u path
替换为真实路径)。嗯,我不知道derby.jar
安装在您的系统上的确切位置,但是您必须提到derby.jar
的完整路径,比如/usr/share/javadb/lib/derby.jar
。然后可以通过java-cp/usr/share/javadb/lib/derby.jar:启动应用程序。TestDB database.properties
。如果您在您的系统上找不到derby.jar
,您仍然可以从官方网站下载。我在尝试运行后更新了问题。把这个测试改为在Apache服务器上使用mysql DB怎么样。我应该用哪个司机?使用MyPHPAdmin。您需要将derby.jar
包含到类路径中。看看这里:当然你可以切换到MySQL。然后需要适当的驱动程序JAR,将其包含在类路径中并更改驱动程序类。使用MyPHPAdmin,您可以管理MySQL数据库,但这与从Java访问数据库无关。(您可以使用类似于管理各种不同数据库的工具)好的,但这与Java类路径无关。在运行应用程序的命令中,除了您的问题之外,还包括您的derby.jar
(将您的占位符driver\u class\u path
替换为真实路径)。嗯,我不知道derby.jar
安装在您的系统上的确切位置,但是您必须提到derby.jar
的完整路径,比如/usr/share/javadb/lib/derby.jar
。然后可以通过java-cp/usr/share/javadb/lib/derby.jar:启动应用程序。TestDB database.properties
。如果在您的系统上找不到derby.jar
,您仍然可以从官方网站下载它。