java简单操作h2数据库,目前使用sqlite3,所以这里只是备份。
参考:https://www.jianshu.com/p/3663ef3fc93d
1、引用
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
2、示例
public class H2Service {
/**
* 以嵌入式(本地)连接方式连接H2数据库,不能使用相对地址。
*/
private static final String JDBC_URL = "jdbc:h2:~/h2.db";
/**
* 使用TCP/IP的服务器模式(远程连接)方式连接H2数据库(推荐)
*/
// private static final String JDBC_URL = "jdbc:h2:tcp://10.35.14.122/C:/H2/user";
private static final String USER = "root";
private static final String PASSWORD = "root";
private static final String DRIVER_CLASS = "org.h2.Driver";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName(DRIVER_CLASS);
Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
Statement statement = conn.createStatement();
statement.execute("DROP TABLE IF EXISTS USER_INF");
statement.execute("CREATE TABLE USER_INF(id INTEGER PRIMARY KEY, name VARCHAR(100), sex VARCHAR(2))");
statement.executeUpdate("INSERT INTO USER_INF VALUES(1, 'tom', '男') ");
statement.executeUpdate("INSERT INTO USER_INF VALUES(2, 'jack', '女') ");
statement.executeUpdate("INSERT INTO USER_INF VALUES(3, 'marry', '男') ");
statement.executeUpdate("INSERT INTO USER_INF VALUES(4, 'lucy', '男') ");
ResultSet resultSet = statement.executeQuery("select * from USER_INF");
while (resultSet.next()) {
System.out.println(
resultSet.getInt("id") + ", " + resultSet.getString("name") + ", " + resultSet.getString("sex"));
}
statement.close();
conn.close();
}
}
结果: