h2数据库增删改查基本操作
生活随笔
收集整理的這篇文章主要介紹了
h2数据库增删改查基本操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 基礎(chǔ)增刪查改代碼實(shí)踐
- 2 代碼啟動(dòng)h2數(shù)據(jù)庫和初始化
1 基礎(chǔ)增刪查改代碼實(shí)踐
通過jdbc的連接方式,對(duì)server模式的h2數(shù)據(jù)庫進(jìn)行增刪查改操作
public static void main(String[] args) {//String jdbcUrl = "jdbc:h2:C:\\CRroot\\documents\\test";//內(nèi)嵌模式,直接操作文件String jdbcUrl = "jdbc:h2:tcp://localhost/C:\\CRroot\\documents\\test";//tcp模式,需要手動(dòng)打開瀏覽器連接一下String userName = "jeason";String passward = "jeason";Connection testDbConnection = null;/*使用前要先導(dǎo)入h2數(shù)據(jù)庫的驅(qū)動(dòng),本文使用maven中的數(shù)據(jù)庫驅(qū)動(dòng)jar包,如果不在maven中引入 h2.1.4.199.jar則在運(yùn)行時(shí)報(bào)錯(cuò):java.sql.SQLException: No suitable driver found for jdbc:h2:./testdb很顯然是沒有找到h2數(shù)據(jù)庫的驅(qū)動(dòng),那他是如何知道這個(gè)數(shù)據(jù)庫的類型的?可以看一下 Connection getConnection(String url, java.util.Properties info, Class<?> caller) 的源碼看上去好像是遍歷所有注冊(cè)類,一個(gè)接一個(gè)的進(jìn)行連接嘗試*/try {//設(shè)置全局的數(shù)據(jù)庫超時(shí)連接DriverManager.setLoginTimeout(1);testDbConnection = DriverManager.getConnection(jdbcUrl, userName, passward);//一開始感覺就像是把某個(gè)具體的數(shù)據(jù)庫抽象成某個(gè)類的實(shí)例,就想File類一樣//然而,其實(shí)這個(gè)connection就只是一個(gè)“管道”,去進(jìn)行增刪查改等操作還需要出通過statement和prepareStatement等借口實(shí)現(xiàn)//顯式加載驅(qū)動(dòng)類,對(duì)于h2數(shù)據(jù)庫可有可無,如果不顯式加載,有些編譯器會(huì)優(yōu)化掉沒有使用的類Class.forName("org.h2.Driver");System.out.println(testDbConnection.isClosed());//Statement的實(shí)例對(duì)象用來執(zhí)行相關(guān)的SQL語句Statement statement = testDbConnection.createStatement();//開始對(duì)數(shù)據(jù)庫進(jìn)行一些操作//如果存在相應(yīng)的表,則刪除statement.execute("drop table test_table if exists");//創(chuàng)建表statement.execute("create table test_table" +"(id int primary key,name varchar(100),age int,sex varchar(100))");//新增數(shù)據(jù)for (int i = 0; i < 10; i++) {String sqlStatement = String.format("insert into test_table(id,name,age,sex) values(%s,%s,%s,%s)",String.valueOf(i), '\u039A' + (char) i + '\u039A', 10 * i, '\u039A'+'男'+'\u039A');System.out.println(sqlStatement);statement.execute(sqlStatement);}//查詢結(jié)果System.out.println("開始查詢并打印數(shù)據(jù)庫內(nèi)容:");ResultSet resultSet = statement.executeQuery("select * from test_table");while (resultSet.next()) {System.out.println(resultSet.getInt(1) + "--" +resultSet.getString(2) + "--" +resultSet.getInt(3) + "--" +resultSet.getString(4));}//顯式釋放資源statement.close();testDbConnection.close();} catch (SQLException | ClassNotFoundException e) {e.printStackTrace();}}貼一下運(yùn)行結(jié)果:
false insert into test_table(id,name,age,sex) values(0,1844,0,31851) insert into test_table(id,name,age,sex) values(1,1845,10,31851) insert into test_table(id,name,age,sex) values(2,1846,20,31851) insert into test_table(id,name,age,sex) values(3,1847,30,31851) insert into test_table(id,name,age,sex) values(4,1848,40,31851) insert into test_table(id,name,age,sex) values(5,1849,50,31851) insert into test_table(id,name,age,sex) values(6,1850,60,31851) insert into test_table(id,name,age,sex) values(7,1851,70,31851) insert into test_table(id,name,age,sex) values(8,1852,80,31851) insert into test_table(id,name,age,sex) values(9,1853,90,31851) 開始查詢并打印數(shù)據(jù)庫內(nèi)容: 0--1844--0--31851 1--1845--10--31851 2--1846--20--31851 3--1847--30--31851 4--1848--40--31851 5--1849--50--31851 6--1850--60--31851 7--1851--70--31851 8--1852--80--31851 9--1853--90--31851Process finished with exit code 02 代碼啟動(dòng)h2數(shù)據(jù)庫和初始化
代碼實(shí)踐的環(huán)節(jié)中,能訪問數(shù)據(jù)庫的前提是,已經(jīng)通過手動(dòng)的方式啟動(dòng)了h2數(shù)據(jù)庫,在web項(xiàng)目,一般都是在項(xiàng)目啟動(dòng)時(shí)將數(shù)據(jù)庫帶起來的,可以參考參考https://blog.csdn.net/mybook201314/article/details/88662603#_73。
總結(jié)
以上是生活随笔為你收集整理的h2数据库增删改查基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让你的Excel更精彩 让你的工作更轻松
- 下一篇: 信息安全服务资质(CCRC)简介