Java嵌入式数据库h2

时间:2016-10-24 05:28:29

标签: java mysql h2

我正在尝试使用h2数据库创建一个简单的应用程序。程序正在完美地工作一次。当我想要插入更多数据时,发生以下错误。

org.h2.jdbc.JdbcSQLException:数据库可能已在使用中:" C:/Users/ali/bookDB.mv.db"。可能的解决方案:关闭所有其他连接;使用服务器模式[90020-186]

java.lang.IllegalStateException:文件被锁定:nio:C:/Users/ali/bookDB.mv.db [1.4.186 / 7]

代码是

package h2_basic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class H2_Basic {

    public static void main(String[] args)  {
       try{ 
        Class.forName("org.h2.Driver");
           Connection con = DriverManager.getConnection("jdbc:h2:~/bookDB","test","test");
           Statement sta =  con.createStatement();
            String CREATE_TABLE = "CREATE TABLE BOOKS "
            + "(bookid bigint auto_increment NOT NULL PRIMARY KEY, "
            + " booktitle VARCHAR(255), "
            + " bookauthor VARCHAR(255), "
            + " editiondate VARCHAR(255))";

           sta.execute(CREATE_TABLE);

           String sql = "INSERT INTO BOOKS (booktitle, bookauthor, editiondate) VALUES ('ali','ali','12')";
           sta.execute(sql);
       }catch(Exception ex)
       {
          ex.printStackTrace();
       }
    }

}

1 个答案:

答案 0 :(得分:0)

将您的JDBC URL更改为jdbc:h2:~/bookDB;AUTO_SERVER=TRUE,如

DriverManager.getConnection("jdbc:h2:~/bookDB;AUTO_SERVER=TRUE","test","test");

Automatic Mixed Mode中启动H2。