java的h2是什么_java 简单的H2数据库工具类详解

package db;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.logging.Level;

import java.util.logging.Logger;

import org.h2.tools.DeleteDbFiles;

public class DbUtil {

private static Connection myConnection = null;

static {

try {

Class.forName("org.h2.Driver");

} catch (ClassNotFoundException ex) {

Logger.getLogger(DbUtil.class.getName()).log(Level.SEVERE, null, ex);

}

}

public static void setupConn(String theDbPath) throws SQLException {

if(null == myConnection || myConnection.isClosed()) {

myConnection = DriverManager.getConnection("jdbc:h2:"+theDbPath);

}

}

public static Statement getStatement() throws SQLException {

if(null==myConnection || myConnection.isClosed()) {

SQLException ex = new SQLException("No valid database connection!");

Logger.getLogger(DbUtil.class.getName()).log(Level.SEVERE, null, ex);

throw ex;

}

return myConnection.createStatement();

}

public static void closeConn() throws SQLException {

myConnection.close();

}

public static void setupDB(String theDbPath) throws SQLException {

setupConn(theDbPath);

runScript("init.sql");

}

public static void runScript(String thePath) throws SQLException {

Statement stat = getStatement();

stat.execute("runscript from '"+thePath+"'");

stat.close();

}

public static void resetDB(String theDbPath) throws Exception {

// to separate the dbname from the path

int lastSlash = theDbPath.lastIndexOf('/');

DeleteDbFiles.execute(theDbPath.substring(0,lastSlash),

theDbPath.substring(lastSlash),

true);

setupDB(theDbPath);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值