Click here to Skip to main content
15,947,476 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this error "
java.sql.SQLNonTransientConnectionException: The connection was refused because the database AnimeFigurineCollection [wimmy on WIMMY] was not found.
" keeps appearing even tho my database is connected, also when remove my derbyclient.jar this error disappears. idont know whats happening pls help


What I have tried:

this is my code so far


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
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
    
    public void insertProduct(Collection col){
    try {
        String queryStr = "Insert into ALBUM(Code, RegistrationNumber, Barcode, MainCharName, Title, Version, Type)"
                + "Values(?,?,?,?,?,?,?)";
        
        ps = conn.prepareStatement(queryStr);
        ps.setString(1, col.getCode());
        ps.setInt(2, col.getRegistrationNumber());
        ps.setInt(3, col.getBarcode());
        ps.setString(4, col.getMaincharName());
        ps.setString(5, col.getTitle());
        ps.setString(6, col.getVersion());
        ps.setString(7, col.getSize());
        
        ps.execute();
        JOptionPane.showMessageDialog(null, "Successfully added " + col.getCode() + ", " 
                + col.getMaincharName() + ", " + col.getTitle()+ ", " + col.getVersion() + ", " + col.getSize());
    } catch (SQLException ex) {
        java.util.logging.Logger.getLogger(CollectionRepository.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    
    }
    
    public void editProduct(Collection col) throws SQLException {
              
        String queryStr = "UPDATE ALBUM"
                + " SET registrationNumber=?, barcode=?, maincharName=?, title=?, version=?, type=?"
                + " WHERE code=?";
        
        ps = conn.prepareStatement(queryStr);
        
        ps.setInt(1, col.getRegistrationNumber());
        ps.setInt(2 , col.getBarcode());
        ps.setString(3, col.getMaincharName());
        ps.setString(4, col.getTitle());
        ps.setString(5, col.getVersion());
        ps.setString(6, col.getSize());
        ps.setString(7, col.getCode());
        
        ps.execute();
        JOptionPane.showMessageDialog(null, "Successfully updated " + col.getMaincharName() + ", " + col.getTitle());
    }
    
               
    public void deleteStudent(String code) throws SQLException {
               
        String queryStr = "DELETE FROM ALBUM"
                + " WHERE code=?";
        
        ps = conn.prepareStatement(queryStr);
        ps.setString(1, code);
        ps.execute();
        JOptionPane.showMessageDialog(null, "Successfully deleted ");
       
    }//end delete
}
Posted
Updated 9-Aug-23 2:49am
Comments
Richard MacCutchan 9-Aug-23 6:33am    
This would appear to be an issue with the derby driver.

1 solution

You are using square brackets and spaces in the database URL, which is causing the connection to be refused. The URL format for connecting to a Derby database should not include square brackets or spaces.

Also, it seems that you are using the Derby client driver 'org.apache.derby.jdbc.ClientDriver' to connect to the database. If you are running the Derby database locally (in embedded mode), you should use the embedded driver instead, you would need to include the 'derby.jar' in your project's classpath.

The DB Connection URL format is discussed at - Apache | Database connection URLs[^]

To fix your error, replace this line -
Java
String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection [wimmy on WIMMY]";


with this line -
Java
String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection";


Replace this line -
Java
String driver = "org.apache.derby.jdbc.ClientDriver";

with this line -
Java
String driver = "org.apache.derby.jdbc.EmbeddedDriver";


Your connection will then look like this -
Java
public void dbconn() throws SQLException, ClassNotFoundException {
    String url = "jdbc:derby://localhost:1527/AnimeFigurineCollection";
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    Class.forName(driver);
    conn = DriverManager.getConnection(url, "wimmy", "1231");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900