Java:H2数据库使用示例
【摘要】 参考文档:http://h2database.com/html/main.html
依赖
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version...
参考文档:http://h2database.com/html/main.html
依赖
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
代码示例
package com.demo.h2;
import java.sql.*;
public class H2Demo { public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = "jdbc:h2:./database"; String driverClass = "org.h2.Driver"; Class.forName(driverClass); Connection connection = DriverManager.getConnection(url); Statement statement = connection.createStatement(); // 创建数据表 statement.execute("DROP TABLE IF EXISTS user"); statement.execute("create table user(id int(11) primary key auto_increment, name varchar(20))"); // 添加数据 statement.executeUpdate("insert into user(name) values('刘备')"); statement.executeUpdate("insert into user(name) values('关羽')"); statement.executeUpdate("insert into user(name) values('张飞')"); // 查询数据 ResultSet resultSet = statement.executeQuery("select * from user"); while (resultSet.next()){ Integer id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println("id: " + id + ", name: " + name); } // 关闭链接 resultSet.close(); statement.close(); connection.close(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
使用IDEA打开的时候,如果连接不上,可以试试配置路径不要写后缀database.mv.db
-> database
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/110942771
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)