What to do to make the HSQL driver working?(如何使 HSQL 驱动程序正常工作?)
问题描述
我目前正在学习 Java 中的一些数据库技巧,并且我发现我正在阅读这本不错的书.在某些时候,它鼓励我尝试使用以下类手动连接数据库:
import java.sql.DriverManager;导入java.sql.Connection;导入 java.sql.PreparedStatement;导入java.sql.ResultSet;公共类 DemoSelect {公共静态无效主要(最终字符串 [] 参数){//连接参数字符串 usr = "sa";字符串密码 = "";字符串驱动 = "org.hsqldb.jdbcDriver";字符串 url = "jdbc:hsqldb:hsql://localhost/xdb";连接 con = null;PreparedStatement pstm = null;结果集 rs = null;尝试 {//启动驱动程序Class.forName(驱动程序);//连接con = DriverManager.getConnection(url, usr, pwd);//编写查询String sql = "SELECT empno、ename、hiredate、deptno FROM emp";//设置 SQL 语句pstm = con.prepareStatement(sql);//执行 SQL 查询rs = pstm.executeQuery();//迭代结果而(rs.next()){//显示数据的简单方法System.out.print(rs.getInt("empno") + ", ");System.out.print(rs.getString("ename") + ", ");System.out.print(rs.getDate("hiredate") + ", ");System.out.println(rs.getInt("deptno"));}} 捕捉(最终异常 e){e.printStackTrace();抛出新的 RuntimeException(e);} 最后 {尝试 {//关闭所有打开的资源if (rs != null) rs.close();if (pstm != null) pstm.close();if (con != null) con.close();} 捕捉(最终异常 e){e.printStackTrace();抛出新的 RuntimeException(e);}}}}
但是当我尝试运行它时,它会产生一个错误,描述如下:
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver在 java.net.URLClassLoader$1.run(URLClassLoader.java:366)在 java.net.URLClassLoader$1.run(URLClassLoader.java:355)在 java.security.AccessController.doPrivileged(本机方法)在 java.net.URLClassLoader.findClass(URLClassLoader.java:354)在 java.lang.ClassLoader.loadClass(ClassLoader.java:423)在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)在 java.lang.ClassLoader.loadClass(ClassLoader.java:356)在 java.lang.Class.forName0(本机方法)在 java.lang.Class.forName(Class.java:186)在 cap3.jdbc.DemoSelect.main(DemoSelect.java:25)线程主"java.lang.RuntimeException 中的异常:java.lang.ClassNotFoundException:org.hsqldb.jdbcDriver在 cap3.jdbc.DemoSelect.main(DemoSelect.java:53)引起:java.lang.ClassNotFoundException:org.hsqldb.jdbcDriver在 java.net.URLClassLoader$1.run(URLClassLoader.java:366)在 java.net.URLClassLoader$1.run(URLClassLoader.java:355)在 java.security.AccessController.doPrivileged(本机方法)在 java.net.URLClassLoader.findClass(URLClassLoader.java:354)在 java.lang.ClassLoader.loadClass(ClassLoader.java:423)在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)在 java.lang.ClassLoader.loadClass(ClassLoader.java:356)在 java.lang.Class.forName0(本机方法)在 java.lang.Class.forName(Class.java:186)在 cap3.jdbc.DemoSelect.main(DemoSelect.java:25)Java 结果:1构建成功(总时间:0 秒)`
我可能会错过什么?
我正在使用 NetBeans,我下载了 HSQL 驱动程序.我设法将 .jar 文件导入到库中,并收到以下错误代码:
java.sql.SQLTransientConnectionException:java.net.ConnectException:连接被拒绝:连接在 org.hsqldb.jdbc.Util.sqlException(未知来源)在 org.hsqldb.jdbc.Util.sqlException(未知来源)在 org.hsqldb.jdbc.JDBCConnection.<init>(未知来源)在 org.hsqldb.jdbc.JDBCDriver.getConnection(未知来源)在 org.hsqldb.jdbc.JDBCDriver.connect(未知来源)在 java.sql.DriverManager.getConnection(DriverManager.java:579)在 java.sql.DriverManager.getConnection(DriverManager.java:221)在 cap3.jdbc.DemoSelect.main(DemoSelect.java:28)引起:org.hsqldb.HsqlException:java.net.ConnectException:连接被拒绝:连接在 org.hsqldb.ClientConnection.openConnection(未知来源)在 org.hsqldb.ClientConnection.initConnection(未知来源)在 org.hsqldb.ClientConnection.<init>(未知来源)... 6 更多引起:java.net.ConnectException:连接被拒绝:连接在 java.net.DualStackPlainSocketImpl.connect0(本机方法)在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)在 java.net.Socket.connect(Socket.java:579)在 java.net.Socket.connect(Socket.java:528)在 java.net.Socket.<init>(Socket.java:425)在 java.net.Socket.<init>(Socket.java:208)在 org.hsqldb.server.HsqlSocketFactory.createSocket(未知来源)... 9 更多线程主"java.lang.RuntimeException 中的异常:java.sql.SQLTransientConnectionException:java.net.ConnectException:连接被拒绝:连接在 cap3.jdbc.DemoSelect.main(DemoSelect.java:53)引起:java.sql.SQLTransientConnectionException:java.net.ConnectException:连接被拒绝:连接在 org.hsqldb.jdbc.Util.sqlException(未知来源)在 org.hsqldb.jdbc.Util.sqlException(未知来源)在 org.hsqldb.jdbc.JDBCConnection.<init>(未知来源)在 org.hsqldb.jdbc.JDBCDriver.getConnection(未知来源)在 org.hsqldb.jdbc.JDBCDriver.connect(未知来源)在 java.sql.DriverManager.getConnection(DriverManager.java:579)在 java.sql.DriverManager.getConnection(DriverManager.java:221)在 cap3.jdbc.DemoSelect.main(DemoSelect.java:28)引起:org.hsqldb.HsqlException:java.net.ConnectException:连接被拒绝:连接在 org.hsqldb.ClientConnection.openConnection(未知来源)在 org.hsqldb.ClientConnection.initConnection(未知来源)在 org.hsqldb.ClientConnection.<init>(未知来源)... 6 更多引起:java.net.ConnectException:连接被拒绝:连接在 java.net.DualStackPlainSocketImpl.connect0(本机方法)在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)在 java.net.Socket.connect(Socket.java:579)在 java.net.Socket.connect(Socket.java:528)在 java.net.Socket.<init>(Socket.java:425)在 java.net.Socket.<init>(Socket.java:208)在 org.hsqldb.server.HsqlSocketFactory.createSocket(未知来源)... 9 更多Java 结果:1构建成功(总时间:1 秒)
HSQL 驱动程序不在您的类路径中.您必须以某种方式将该驱动程序 jar 文件包含到 it 通过使用 java -cp .;hsqldb.jar DemoSelect
运行您的程序.如果您在 *nix 上,请使用 ':
' 作为分隔符,而不是 ';
'.
另请参阅文档.p>
I'm currently learning some database tricks in Java and I found this nice book I'm reading. At some point, it encourages me to try a manual database connection with the following class:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DemoSelect {
public static void main(final String[] arguments) {
// Connection parameters
String usr = "sa";
String pwd = "";
String driver = "org.hsqldb.jdbcDriver";
String url = "jdbc:hsqldb:hsql://localhost/xdb";
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
// Starting up the driver
Class.forName(driver);
// Connecting
con = DriverManager.getConnection(url, usr, pwd);
// Writing a query
String sql = "SELECT empno, ename, hiredate, deptno FROM emp";
// Setting up the SQL statement
pstm = con.prepareStatement(sql);
// Execute the SQL query
rs = pstm.executeQuery();
// Iterating the results
while (rs.next()) {
// Simple method to show the data
System.out.print(rs.getInt("empno") + ", ");
System.out.print(rs.getString("ename") + ", ");
System.out.print(rs.getDate("hiredate") + ", ");
System.out.println(rs.getInt("deptno"));
}
} catch(final Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
try {
// Closing all the opened resources
if (rs != null) rs.close();
if (pstm != null) pstm.close();
if (con != null) con.close();
} catch(final Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
But when I try to run it, it generates an error with the following description:
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at cap3.jdbc.DemoSelect.main(DemoSelect.java:25)
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
at cap3.jdbc.DemoSelect.main(DemoSelect.java:53)
Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at cap3.jdbc.DemoSelect.main(DemoSelect.java:25)
Java Result: 1
BUILD SUCCESSFUL(total time: 0 seconds)`
What could I be missing?
I'm using NetBeans and I downloaded the HSQL Driver. I managed to import the .jar file to the libraries and I got the following error code:
java.sql.SQLTransientConnectionException: java.net.ConnectException: Connection refused: connect
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at cap3.jdbc.DemoSelect.main(DemoSelect.java:28)
Caused by: org.hsqldb.HsqlException: java.net.ConnectException: Connection refused: connect
at org.hsqldb.ClientConnection.openConnection(Unknown Source)
at org.hsqldb.ClientConnection.initConnection(Unknown Source)
at org.hsqldb.ClientConnection.<init>(Unknown Source)
... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at org.hsqldb.server.HsqlSocketFactory.createSocket(Unknown Source)
... 9 more
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLTransientConnectionException: java.net.ConnectException: Connection refused: connect
at cap3.jdbc.DemoSelect.main(DemoSelect.java:53)
Caused by: java.sql.SQLTransientConnectionException: java.net.ConnectException: Connection refused: connect
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at cap3.jdbc.DemoSelect.main(DemoSelect.java:28)
Caused by: org.hsqldb.HsqlException: java.net.ConnectException: Connection refused: connect
at org.hsqldb.ClientConnection.openConnection(Unknown Source)
at org.hsqldb.ClientConnection.initConnection(Unknown Source)
at org.hsqldb.ClientConnection.<init>(Unknown Source)
... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at org.hsqldb.server.HsqlSocketFactory.createSocket(Unknown Source)
... 9 more
Java Result: 1
BUILD SUCCESSFUL(total time: 1 second)
The HSQL driver is not in your classpath. You'll have to somehow include that driver jar file to it by running your program with java -cp .;hsqldb.jar DemoSelect
. Use ':
' as a separator instead of ';
', if you're on *nix.
See also the documentation.
这篇关于如何使 HSQL 驱动程序正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 HSQL 驱动程序正常工作?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01