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

问题描述

我目前正在学习Java中的一些DataBase技巧,我发现这本漂亮的书我正在读。
在某些时候,ite鼓励我尝试与以下类的手动DataBase连接:

I'm currently learning some DataBase tricks in Java and I found this nice book I'm reading.At some point, ite 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 (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);

            }

        }

    }

}

但是当我尝试运行它时,会产生一个错误,并带有以下描述:

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

$ b b

我可以丢失什么?

What could i be missing?

我使用的是NetBeans,我下载了HSQL驱动程序。我设法导入.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驱动程序不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 21:44