简体   繁体   中英

DB Derby Database stops functioning once connected to DBeaver

Here is my application's code to create the database, connect to it, and make a table in the database called Accounts.

package eportfolio.application;

import java.io.File;
import java.io.FileWriter;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;


/**
 *
 * @author valeriomacpro
 */
public class HomePage extends javax.swing.JFrame {
    public static String username;
    public static String password;
    public static int SelectedPost;
    /**
     * Creates new form HomePage
     */
    
    public static boolean doesTableExists (String tableName, Connection conn)
            
                throws SQLException {
                DatabaseMetaData meta = conn.getMetaData();
                ResultSet result = meta.getTables(null, null, tableName.toUpperCase(), null);
        
        return result.next();
    }
    
    
    public HomePage() {
        initComponents();
        
        try
        {
                String databaseURL = "jdbc:derby:eportdatabase;create=true";
                Connection con = DriverManager.getConnection(databaseURL);
                Statement st = con.createStatement();
            
                if (!doesTableExists("Accounts", con))
                {
                    String sql = "CREATE TABLE Accounts (Username varchar(250), Password varchar(250)) ";
                    st.execute(sql);
                    System.out.println("Table Does Not Yet Exist!");
                }
                else if(doesTableExists("Accounts", con)) {
                    System.out.println("Table Already Exists!");
                }
                
                
                con.close();
        } catch(SQLException e) {
            do {
                System.out.println("SQLState:" + e.getSQLState());
                System.out.println("Error Code:" + e.getErrorCode());
                System.out.println("Message:" + e.getMessage());
                Throwable t = e.getCause();
                while(t != null) {
                System.out.println("Cause:" + t);
                t = t.getCause();
            }
            e = e.getNextException();
        } while (e != null);
    }
}

Additionally, here is my code that interacts with the Accounts table.

        try
        {
                String databaseURL = "jdbc:derby:eportdatabase;";
                Connection con1 = DriverManager.getConnection(databaseURL);
                Statement st = con1.createStatement();
                
                
                String sql = " INSERT INTO Accounts VALUES ('"+txtNewUsername.getText()+"','"+txtNewPassword.getText()+"') ";
                st.executeUpdate(sql);
            
                JOptionPane.showMessageDialog(null, "Account Info Saved!"); 
                txtNewUsername.setText("");
                txtNewPassword.setText("");
                txtNewConfirm.setText("");
                

        }

When I run the application, the code works fine. However, if I open DBeaver and connect it to my database, then the following error message comes up. Does not come up if DBeaver is closed, even if it is connected to the database.

Message:Failed to start database 'eportdatabase' with class loader jdk.internal.loader.ClassLoaders$AppClassLoader@45ee12a7, see the next exception for details.
Cause:ERROR XJ040: Failed to start database 'eportdatabase' with class loader jdk.internal.loader.ClassLoaders$AppClassLoader@45ee12a7, see the next exception for details.
Cause:ERROR XSDB6: Another instance of Derby may have already booted the database /Users/(username)/NetBeansProjects/ePortfolio Application/eportdatabase.
SQLState:XSDB6
Error Code:45000
Message:Another instance of Derby may have already booted the database /Users/(username)/NetBeansProjects/ePortfolio Application/eportdatabase.
Cause:ERROR XSDB6: Another instance of Derby may have already booted the database /Users/(username)/NetBeansProjects/ePortfolio Application/eportdatabase.

Why is this? Am I connecting the Database to DBeaver incorrectly? Or am I coding the database incorrectly in Netbeans? It could be that my drivers and db derby version are old, but I have not been able to find help on that online either. Also important to know that the table does show up in DBeaver, but does not update. I have to delete the database folder in my application's folder every time I want to use the application with DBeaver open. Any help appreciated.

By using this line of code:

String databaseURL = "jdbc:derby:eportdatabase;";

you are using Derby in the "embedded" configuration. With Embedded Derby, only one Java application at a time can use the database. Other applications that try to use it concurrently are rejected with the message

Another instance of Derby may have already booted the database

as you saw when you tried it.

There are other configurations in which Derby can be deployed and run; specifically there is a Client-Server configuration in which multiple applications may all run as clients, and may connect to the same Derby server, allowing the applications to run concurrently.

To learn more about these aspects of Derby, start here: https://db.apache.org/derby/docs/10.15/getstart/cgsquck70629.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM