数据库概述05(数据库查询及修改操作)
數(shù)據(jù)庫查詢及修改操作
可修改的結(jié)果集
由于執(zhí)行性能的問題,一般不建議使用
public static void main(String[] args)throws Exception {Class.forName("com.mysql.cj.jdbc.Driver");String sql = "select * from tb_emp";try(Connection conn = DriverManager.getConnection("jdbc:mysql:///test?serverTimezone=UTC","root","123456");PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);ResultSet rs = ps.executeQuery()){while (rs.next()) {System.out.println(rs.getLong("id") + "\t" + rs.getString("name"));//修改數(shù)據(jù)if (rs.getDouble("salary") < 1000) {double oldValue = rs.getDouble("salary");rs.updateDouble("salary", oldValue + 200);rs.updateRow();}}//插入數(shù)據(jù)rs.moveToInsertRow();rs.updateString("name","傻子");rs.updateDouble("salary", 345.67);rs.insertRow();//刪除數(shù)據(jù)rs.absolute(4);rs.deleteRow();}模糊查詢問題
模糊查詢:select * from tb_emp where name like ‘%龍%’
問題:sql=“select * from tb_emp where name like ‘%?%’”;報(bào)錯(cuò)
- 解決方案1:
String sql=“select * from tb_emp where name like ?”;
preparedStatement.setString(1,“%龍%”);
like ‘a(chǎn)bc’ 和 ='abc’從查詢結(jié)果上來說,沒有區(qū)別
如果使用模糊查詢則傳入?yún)?shù)包含通配符;如果是精確等值查詢,則不包含通配符 - 解決方案2:
使用系統(tǒng)提供的函數(shù)concat用于實(shí)現(xiàn)字符串的拼接操作
String sql=“select * from tb_emp where name like concat(‘%’,?,‘%’)”;
preparedStatement.setString(1,“龍”);
in的用法
- 方法一
- 方法二
獲取數(shù)據(jù)表的元數(shù)據(jù)
ResultSetMetaData對象保存了所有ResultSet對象中關(guān)于字段的信息,提供了對應(yīng)的方法獲取字段相關(guān)的信息
- int getColumnCount()獲取ResultSet對象中的字段個(gè)數(shù)
- String getColumnName(int index)獲取ResultSet對象中指定序號對應(yīng)字段的名稱
SQLException
調(diào)用JDBC API方法時(shí)經(jīng)常會有一個(gè)受檢型異常/非運(yùn)行時(shí)異常,必須在編碼過程中針對異常進(jìn)行處理。但是大部分出現(xiàn)的異常
是不能通過編碼解決,例如SQL語句語法錯(cuò)誤、關(guān)閉Connection對象時(shí)出錯(cuò)。而且SQLException的顆粒度太粗,不能明確
表達(dá)異常的原因。
所以常見的處理方案有2種:
1、向上拋出異?;蛘哂涗泩?bào)錯(cuò)信息。
2、將SQLException轉(zhuǎn)換為運(yùn)行時(shí)異常,由上層調(diào)用方進(jìn)行處理
練習(xí)
產(chǎn)品管理系統(tǒng)
- 類目
- 產(chǎn)品
岐山香醋 800ml 20220714 調(diào)味品
精碘鹽 500g 20200315 調(diào)味品
茅臺 500ml 20001230 酒精飲料
create table t1(
id bigint primary key auto_increment, – 代理主鍵
name varchar(20) not null unique, – 真實(shí)開發(fā)中一般不使用pk之外的其它約束
rong int,
unit char(2) default ‘ml’,
pdate date,
catalog varchar(20)
)engine=innodb default charset utf8;
獲取所有商品種類信息: select distinct catalog from t_product;
如果將類目信息添加到產(chǎn)品表中,則會產(chǎn)生數(shù)據(jù)冗余,會有增刪改異常
所以創(chuàng)建2個(gè)表,一個(gè)表用于存儲類目信息,一個(gè)表用于存儲產(chǎn)品信息,通過產(chǎn)品表中的外鍵表示對應(yīng)的類目信息
create table t_catalog(
id bigint primary key auto_increment,
name varchar(32) not null
);
create table t_product(
id bigint primary key auto_increment,
name varchar(32) not null,
…
catalog_id bigint comment ‘對應(yīng)商品的類目編號’
foreign key(catalog_id) references t_catalog(id) on delete cascade
);
foreign key用于定義外鍵約束【參照完整性】,要求外鍵列中可取的值,必須在主表中已經(jīng)存在
foreign key(catalog_id) references t_catalog(id) on delete cascade表示當(dāng)前表的catalog_id列的取值范圍只能是t_catalog表的id列
中已經(jīng)存在的值。注意外鍵沒有默認(rèn)的not null含義
學(xué)生信息和班級信息
create table t_clz(
id bigint primary key auto_increment,
title varchar(32) not null,
address varchar(50) comment ‘固定的晚自習(xí)教室’
) engine=innodb default charset utf8;
insert into t_clz(id,title) values(1,‘110201’),(2,‘110302’),(3,‘110301’);
create table t_student(
id bigint primary key auto_increment,
name varchar(20) not null,
clz_id bigint not null,
foreign key(clz_id) references t_clz(id) on delete cascade
)engine=innodb default charset utf8;
insert into t_student values(null,‘內(nèi)蒙人’,1);
insert into t_student values(null,‘王建國’,1);
insert into t_student values(null,‘干一票’,2);
insert into t_student values(null,‘強(qiáng)強(qiáng)’,3);
獲取學(xué)生信息和對應(yīng)的班級信息
select * from t_student s,t_clz z where s.clz_id=z.id;
為了簡化查詢語句引入視圖定義
mysql> create view v_clz_stu as select s.*,z.title from t_student s,t_clz z where s.clz_id=z.id;
總結(jié)
以上是生活随笔為你收集整理的数据库概述05(数据库查询及修改操作)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站怎么做SEO优化操作?
- 下一篇: character在mysql什么类型_