开发者

What to do to make the HSQL driver working?

开发者 https://www.devze.com 2023-04-06 00:22 出处:网络
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:

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.h开发者_StackOverflow中文版sqldb.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.


The second try of yours is trying to connect to the network server. So, you have to start the network server first.

You can follow my answer in this post to play with the HSQL server.


Q: What could I be missing?

A: It sounds like you're missing the HSQL driver, or perhaps even HSQL itself :)

Q: Are you sure HSQL is successfully installed on your system? Have a look here.

Q: Are you including the HSQL JDBC driver in your classpath? Or, if you're using an IDE, do the "library" settings include the HSQL JDBC driver?

Here's a good link for adding 3rd party .jar files (like HDBC) to an Eclipse project.

And here's a link on different HSQL options for your JDBC connection.

Are you using Eclipse, Netbeans, or "something else"?


For me, the problem was in the classpath. I'm working with eclipse tomcat and hslqdb.jar.

Select "run configurations..." -> tomcat server -> classpath -> user entries -> add jar

May be you forgot to add hsqldb.jar to classpath for the server.


This can also happen if you accidentally comment out hsql.jdbc.driver=org.hsqldb.jdbcDriver from your properties files. :-D


The IP address of the host PC should be used instead of localhost/127.0.0.1.


Just adding your mention stuff didnt help.

"Select "run configurations..." -> tomcat server -> classpath -> user entries -> add jar"

It was a good start but you MUST ALSO INCLUDE THE sqltool.jar.

0

精彩评论

暂无评论...
验证码 换一张
取 消