com.h2database
h2
1.4.200
runtime
h2数据库的连接和MySQL连接一样,都可以用jdbc去获取,只需要修改驱动类、URL和用户名密码
H2通常用tcp的方式连接,也可以本地连接,URL根据登录界面的URL修改就行了
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class H2Utils {
// 定义连接常量
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/D:/Program Files (x86)/H2/bin/sea";
private static final String USER = "sa";
private static final String PASSWORD = "";
public static Connection conn = getConnection();
// 测试
public static void main(String[] args) throws SQLException {
System.out.println(conn);
}
// jdbc连接H2数据库
private static Connection getConnection() {
if (null != conn) return conn;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
if (null != conn) System.out.println("H2数据库连接成功!");
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}