首页 > 解决方案 > 找不到适合 jdbc:hsqldb:file:\dir\to\db 的驱动程序

问题描述


我想在我的 Vaadin-Project 中使用 HSQLDB,但它不起作用。我的连接代码如下所示:

package ml.uniwide.ufin.spring;
import java.sql.*;

public class MySQL { // I know that I use HSQLDB not MySQL...
    public boolean auth(String username, String password) {
        Connection conn = null;
        try {
              Class.forName( "org.hsqldb.jdbcDriver" );
            }
            catch ( ClassNotFoundException e ) {
              System.err.println( "no driver found" );
            }
        String url = "jdbc:hsqldb:file:D:\\Dokumente\\UNIWIDEDatabase";
        try {
            Connection con = DriverManager.getConnection( url, "user", "pw" );

            Statement stmt = con.createStatement();
            String selectUserName = "SELECT * FROM \"PUBLIC\".\"USERS\" WHERE USERNAME = '" + username + "' AND PASSWORDHASH = '" + password + "'";
            ResultSet result = stmt.executeQuery(selectUserName);
            int count = 0;
            while(result.next()) {
                count++;
            }
            if(count<1) {
                return false;
            } else {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}

我还添加到 pom.xml 文件中:

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.5.0</version>
</dependency>

我还尝试将外部 hsqldb.jar 文件包含到 Eclipse 中,但输出始终是:

no driver found
java.sql.SQLException: No suitable driver found for jdbc:hsqldb:file:D:\Dokumente\UNIWIDEDatabase

有人知道这个问题的解决方案吗?非常感谢

标签: javadatabasespringvaadinhsqldb

解决方案


您是否启动了 HSQLDB 服务?在建立任何连接之前,它需要通过其管理器工具或以编程方式通过如下代码启动:

public class DBManager {

final String dbLocation = "D:\\Dokumente\\UNIWIDEDatabase"; // change it to your db location
org.hsqldb.Server sonicServer;
Connection dbConn = null;

public static void main(String[] args) {
    DBManager manager = new DBManager();
    manager.startDBServer();
}

public void startDBServer() {
    HsqlProperties props = new HsqlProperties();
    props.setProperty("server.database.0", "file:" + dbLocation + ";");
    props.setProperty("server.dbname.0", "xdb");
    sonicServer = new org.hsqldb.Server();
    try {
        sonicServer.setProperties(props);
    } catch (Exception e) {
        return;
    }
    sonicServer.start();
}

public void stopDBServer() {
    sonicServer.shutdown();
}

public Connection getDBConn() {
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        dbConn = DriverManager.getConnection(
                "jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dbConn;
}

} 然后是你连接的 URL"jdbc:hsqldb:hsql://localhost/xdb"


推荐阅读