本文介绍了如何使HSQL驱动程序正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Java中的一些数据库技巧,并且发现了我正在阅读的这本好书。
在某些时候,它鼓励我尝试使用以下类进行手动数据库连接:

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)`

我可能会缺少什么?

我正在使用NetBeans,并下载了HSQL Dr艾弗我设法将.jar文件导入到库中,并且得到以下错误代码:

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)


推荐答案

HSQL驱动程序不在您的类路径中。您必须以某种方式将该驱动程序jar文件包含到。

这篇关于如何使HSQL驱动程序正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 21:27