首页  

hutool-db hsqldb2 例子     所属分类 hutool 浏览量 199
https://hutoolkit.com/docs/db/simple-operation-db/


cn.hutool:hutool-db:5.8.28
org.hsqldb:hsqldb:2.0.0



src/main/resources/db.setting driver=org.hsqldb.jdbcDriver url=jdbc:hsqldb:mem:memdb1 username=sa password= showSql=true formatSql=false showParams=true sqlLevel=debug [memdb2] driver=org.hsqldb.jdbcDriver url=jdbc:hsqldb:mem:memdb2 username=sa password=
driver=org.hsqldb.jdbcDriver url=jdbc:hsqldb:mem:memdb2
import cn.hutool.db.Db; import cn.hutool.db.Entity; import cn.hutool.db.ds.DSFactory; import javax.sql.DataSource; import java.util.List; private static final String DS1 = "memdb1"; private static final String DS2 = "memdb2"; DataSource ds = DSFactory.get(); DataSource ds2 = DSFactory.get(DS2); System.out.println("ds="+ds); System.out.println("ds2="+ds2); // IDENTITY 自增 ,不支持 AUTO_INCREMENT String sql = "create table pets(id INT IDENTITY PRIMARY KEY ,pet_name varchar(128),PET_DESC varchar(128) )"; int count = Db.use().execute(sql); System.out.println("create table pets done,result="+count); Entity pet = Entity.create("pets") .set("pet_name", "pet_name_001") .set("pet_desc", "pet_desc_001"); Long id = Db.use().insertForGeneratedKey(pet); System.out.println("insertForGeneratedKey done,id="+id); pet = Entity.create("pets") .set("pet_name", "pet_name_002") .set("pet_desc", "pet_desc_002"); id = Db.use().insertForGeneratedKey(pet); System.out.println("insertForGeneratedKey done,id2="+id); // memdb2 // user lacks privilege or object not found: PETS // Db.use(DS2).insertForGeneratedKey(pet); List< Entity> list = Db.use().findAll("pets"); for(Entity item:list){ System.out.println(item); } id = 9L; Entity entity = Db.use().get(Entity.create(TABLE).set("id",id)); System.out.println("id="+id+",entity="+entity); id = 1L; entity = Db.use().get(Entity.create(TABLE).set("id",id)); System.out.println("id="+id+",entity="+entity); id = 1L; Entity recordEntity = Entity.create(TABLE).set("pet_desc","pet_desc_002_new"); Entity whereEntity = Entity.create(TABLE).set("id",id); int updateResult = Db.use().update(recordEntity,whereEntity); System.out.println("id="+id+",update done,updateResult="+updateResult); List< Entity> tables = Db.use().query("select ID,PET_NAME,PET_DESC from pets");
cn.hutool.log.LogFactory: Use [Hutool Console Logging] Logger As Default. [DEBUG] cn.hutool.setting.SettingLoader: Load setting file [classpath:db.setting] [DEBUG] cn.hutool.db.DbUtil: Show sql: [true], format sql: [false], show params: [true], level: [DEBUG] [DEBUG] cn.hutool.db.ds.DSFactory: Use [Hutool-Pooled-DataSource] DataSource As Default ds=cn.hutool.db.ds.DataSourceWrapper@15761df8 ds2=cn.hutool.db.ds.DataSourceWrapper@6ab7a896 [DEBUG] cn.hutool.db.dialect.DialectFactory: Use Dialect: [AnsiSqlDialect]. [DEBUG] cn.hutool.db.sql.SqlLog: [SQL] -> INSERT INTO pets (pet_name, pet_desc) VALUES (?, ?) Params -> [pet_name_001, pet_desc_001] [SQL] -> SELECT * FROM pets Entity {tableName=PETS, fieldNames=[id, pet_name, pet_desc], fields={id=0, pet_name=pet_name_001, pet_desc=pet_desc_001}} Entity {tableName=PETS, fieldNames=[id, pet_name, pet_desc], fields={id=1, pet_name=pet_name_002, pet_desc=pet_desc_002}}
https://gitee.com/dyyx/work2024/tree/master/demo/hutooldbdemo

上一篇     下一篇
hutool SPI serviceloader 实例

hutool 常用方法

hutool使用常见问题

hutool-db 使用

grafana alert Group wait Group interval Repeat interval

MyBatis VS MyBatisPlus