You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复java.sql.SQLNonTransientConnectionException数据库未找到连接拒绝错误

问题:Java连接Derby数据库报错SQLNonTransientConnectionException

遇到java.sql.SQLNonTransientConnectionException错误,提示内容:

"The connection was refused because the database AnimeFigurineCollection was not found."

添加derbyclient.jar到项目依赖后,错误信息变为:

java.sql.SQLNonTransientConnectionException: The connection was refused because the database AnimeFigurineCollection [wimmy on WIMMY] was not found. "ERROR 08004: The connection was refused because the database AnimeFigurineCollection [wimmy on WIMMY] was not found."

移除该jar包后错误消失,相关代码如下:

package case_study_3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class CollectionRepository {
    PreparedStatement ps;
    ResultSet rs;
    Connection conn;

    public CollectionRepository() {
    
    try {
        dbconn();
    } catch (SQLException ex) {
        java.util.logging.Logger.getLogger(CollectionRepository.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(CollectionRepository.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    try {
        conn.createStatement();
    } catch (SQLException ex) {
        java.util.logging.Logger.getLogger(CollectionRepository.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    
    }

   public void dbconn() throws SQLException, ClassNotFoundException {
    String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection [wimmy on WIMMY]";
    String driver = "org.apache.derby.jdbc.ClientDriver";
    Class.forName(driver);
    conn = DriverManager.getConnection(url,"wimmy","1231");

    }//end db conn
    
    public List<Collection> getAllProduct(){
        List<Collection> col = new ArrayList<>();
        
        String queryStr = "Select * From ALBUM";
    try {
        ps = conn.prepareStatement(queryStr);
        rs = ps.executeQuery();
        while(rs.next()){
            Collection collect = new Collection(rs.getString("code"),
                    rs.getInt("registrationNumber"),
                    rs.getInt("barcode"),
                    rs.getString("maincharName"),
                    rs.getString("title"),
                    rs.getString("version"),
                    rs.getString("type"));
            col.add(collect);
        }
    } catch (SQLException ex) {
        java.util.logging.Logger.getLogger(CollectionRepository.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    
    
    }    
    return col;
    
}//end getall

解决方案

  • 修正JDBC URL格式:URL中多余的[wimmy on WIMMY]是错误根源。Derby客户端JDBC URL的正确格式为jdbc:derby://<host>:<port>/<databaseName>,用户名和密码需通过getConnection的单独参数传入,不能拼在URL里。修改后的URL应为:

    String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection";
    
  • 确认数据库状态:先检查Derby服务器是否正常启动,同时确认AnimeFigurineCollection数据库已存在。如果数据库尚未创建,可以在URL末尾添加;create=true参数,让Derby自动创建数据库(仅首次运行时需要):

    String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection;create=true";
    
  • 规范依赖引入:使用Derby客户端模式连接时,必须保留derbyclient.jar在类路径中。移除jar包错误消失是因为项目自动切换到了Derby嵌入式驱动,但两种模式的URL格式不同,长期使用易引发混淆。确保仅引入derbyclient.jar,避免多驱动冲突。

  • 检查连接逻辑:JDBC 4.0及以上版本(Java 6+)无需手动调用Class.forName(driver)加载驱动,但如果保留该代码,需确保驱动类路径正确。同时验证用户名wimmy和密码1231与Derby数据库的配置完全匹配。

内容的提问来源于stack exchange,提问作者nick lester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 19:50:25