- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试运行测试程序来证明 Apache Derby 的安装正常。
按照本教程安装Install Apache Derby on Ubuntu
要运行程序,必须从终端输入:
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();
}
}
}
来自SimpleDataSource类:
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)
更新:
我尝试使用 derby 嵌入式驱动程序。我将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)
如何解决这个问题?
最佳答案
属性文件的形式为
key1=value1
key2=value2
你忘记了 key ,所以你所有的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;user=me;password=mine
.
关于java - 使用 Apache Derby 运行 TestDB 程序 - java.sql.SQLException : The url cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18558074/
我尝试连接到数据库: string ConnectionString = "SERVER=localhost;" + "DATABASE=testdb;
我想在新项目中使用 Spring Data JPA。我基本上遵循https://spring.io/guides/gs/accessing-data-jpa/中描述的设置. 当我启动应用程序或开始单个
Soci 的另一个问题...我想连接我刚刚创建的 testDB,但下面的代码显示 fatal error 。 我这样做了: 在 PostgreSQL 上: 1) CREATE USER robert
我尝试运行测试程序来证明 Apache Derby 的安装正常。 按照本教程安装Install Apache Derby on Ubuntu 要运行程序,必须从终端输入: java -classpat
CREATE TABLE blog ( blog_id BIGINT(255), email VARCHAR(255), blog_title CHAR(255), b
我登录到 PostgreSQL:psql -U postgres 已创建“testdb”:CREATE DATABASE testdb; 退出:\q, 登录“testdb”:psql -d testd
我最近从 4.0.2 升级到了 mongo 4.2.0。在以前的版本中,用户可以访问 system.indexes,但升级后,用户无法访问 system.indexes 集合。用户已具有读写角色。此外
我是一名优秀的程序员,十分优秀!