简体   繁体   中英

Why Am I Getting a “Database may be already in use: null” Error for H2 Database That I Just Implemented?

I am putting together a small Java app that I want to use an embedded database for so I'm using H2. I followed the tutorial to set it up, and ran a test for the first run to see if it worked. I had never run it before so it could not have actually been in use, still when I ran it the first time it threw the following error.

org.h2.jdbc.JdbcSQLException: Database may be already in use: null. Possible solutions: close all other connection(s); use the server mode [90020-196]

Here's what I've written.

It's attached to a submit button the form.

    JButton btnRegisterupdate = new JButton("Register/Update");
    btnRegisterupdate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0){

            try {
                Class.forName("org.h2.Driver");
                Connection connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", " ");

                JOptionPane.showMessageDialog(null, "Connection Awesome");
            } catch (ClassNotFoundException  | SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  

        }
    });
    btnRegisterupdate.setBounds(10, 243, 146, 23);
    frame.getContentPane().add(btnRegisterupdate);
}
}

OK, so the instructions for this require the user to go to the login page and click connect. This remains open unless you click disconnect. Nowhere does it say in the tutorial. Once you click connect to set it up, click disconnect in the browser version.

That's what you have to do.

Click connect to set it up, it takes you to another screen where you can create tables, and so on, but when you're done in there, disconnect.

Run the executable jar file in the bin folder. This will open up in a browser. Click on the Server Setting drop down and change it to Generic H2(Server). Previously it was set to Generic H2(Embedded) that was why it didnt work for me.

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