我目前正在学习Java中的一些数据库技巧,我发现这本书正在阅读。 在某些时候,ite鼓励我尝试使用以下类的手动DataBase连接:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DemoSelect {
public static void main (String[] args){
//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);
//Conecting
con = DriverManager.getConnection (url,usr,pwd);
//Writting 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 (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 (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
但是当我尝试运行它时,它会产生一个错误,其中包含以下描述:
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)`
我能错过什么?
我正在使用NetBeans,我下载了HSQL驱动程序。我设法将.jar文件导入库中,我得到以下错误代码:
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)
答案 0 :(得分:6)
hsql驱动程序不在您的类路径中。您必须以某种方式在classpath中包含该驱动程序jar文件。例如你以这种方式运行你的程序java -cp .;hsqldb.jar DemoSelect
。使用:作为分隔符代替;如果你在* nix。
另请参阅文档here
答案 1 :(得分:1)
答:听起来你错过了HSQL驱动程序,甚至HSQL本身:)
问:您确定在系统上成功安装了HSQL吗?这是一个很好的链接:http://hsqldb.org/web/howto.html
问:您是否在类路径中包含HSQL JDBC驱动程序?或者,如果您使用的是IDE,那么“库”设置是否包含HSQL JDBC驱动程序?
这是将第三方.jar文件(如HDBC)添加到Eclipse项目的良好链接:
http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
这里有一个关于JDBC连接的不同HSQL选项的链接:
http://hsqldb.org/doc/guide/ch04.html
您使用的是Eclipse,Netbeans还是“其他”?
答案 2 :(得分:1)
您的第二次尝试是尝试连接到网络服务器。所以你必须先启动网络服务器。
您可以按照我在这篇文章中的回答来使用hsql server。
Concept of In Memory Database and how to see if my data is being populated in HSQL DB?
答案 3 :(得分:0)
应使用PC(托管应用程序)的IP而不是localhost / 127.0.0.1
答案 4 :(得分:0)
对我来说,问题出在类路径中。我正在使用eclipse tomcat和hslqdb.jar。
选择“运行配置...” - &gt; tomcat服务器 - &gt; classpath - &gt;用户条目 - &gt;添加jar
您可能忘记将hsqldb.jar添加到服务器的类路径中。
答案 5 :(得分:0)
如果您不小心从属性文件中注释了hsql.jdbc.driver=org.hsqldb.jdbcDriver
,也会发生这种情况。 :-D
答案 6 :(得分:-1)
只是添加你提到的东西没有帮助。
“选择”运行配置...“ - &gt; tomcat服务器 - &gt; classpath - &gt;用户条目 - &gt;添加jar”
这是一个好的开始,但你还必须包含sqltool.jar。