当前位置:网站首页>【三层架构及JDBC总结】
【三层架构及JDBC总结】
2022-07-05 08:19:00 【今夜鸣风】
在学习JDBC的过程中,需要注意的是工具类必须和查询字段对应,即使查询方法是通用的,但在面对不同表格的时候也得注意工具类的声明属性是否和查询语句查询时候用到的属性对应,不然无法查询到查询语句所需要的对象信息,也不会有正确的结果。
还有比较关键的一点就是由于表的字符集可能存在差异,为了能正常查询和增删改数据,要与现在properties配置文件里面添加关于字符集的语句说明,如下所示:
url=jdbc:mysql://localhost:3306/homework3?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true关于多可变参数:
除了工具类需要对应之外,为了提高查询方法的通用性引入了可变参数【args……】来代指SQL语句所需要的参数。
当存在可变参数时,args相当于一个数组, 底层系统会先判断它的长度然后创建一个同等长度的数组,之后获取参数就遍历一下数组即可
/**
* 查询
* @param sql
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static List<SelectTools> select1(String sql, Object... args) {
Connection conn = null;
PreparedStatement pre = null;
ResultSet resultSet=null;
try {
conn = JDBCUtils.getConn1();
pre = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++){
pre.setObject(i +1,args[i]);
}//当存在可变参数时,args相当于一个数组,
// 底层系统会先判断它的长度然后创建一个同等长度的数组,之后获取参数就遍历一下数组即可。
resultSet =pre.executeQuery();
// while (resultSet.next()){
// System.out.println(resultSet.getObject(1) + " " + resultSet.getObject(2)); ;
// }
//查询多行数据使用迭代器while(resultSet.next())
ResultSetMetaData rsmd=resultSet.getMetaData();
// System.out.println(rsmd);
int columnCount=rsmd.getColumnCount();
List<SelectTools> list = new ArrayList<>();
while (resultSet.next()){
SelectTools se=new SelectTools();
for (int i = 0; i <columnCount; i++) {
Object columnValue= resultSet.getObject(i+1);
String columnName=rsmd.getColumnLabel(i+1);
Field file= SelectTools.class.getDeclaredField(columnName);
file.setAccessible(true);
file.set(se,columnValue);
}
list.add(se);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource1(conn,pre,resultSet);
}
return null;
}JDBC的六个步骤:
大致分为六大步,分别是:
①加载驱动
②和数据库建立链接
③通过连接创建一个SQL命令发送器Statement(更好用的避免SQL注入的现在使用prepareStatement)
④使用SQL命令发送器向数据库发送SQL命令。
⑤处理结果
⑥关闭资源
三层结构各层之间的关系如下:

边栏推荐
- Talk about the function of magnetic beads in circuits
- 99 multiplication table (C language)
- C WinForm [change the position of the form after running] - Practical Exercise 4
- Live555 push RTSP audio and video stream summary (I) cross compilation
- Basic embedded concepts
- Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
- STM32 --- GPIO configuration & GPIO related library functions
- Void* C is a carrier for realizing polymorphism
- Bootloader implementation of PIC MCU
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
猜你喜欢
![[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code](/img/58/576b6b77509ed7a9bef138f3899e37.jpg)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code

My-basic application 2: my-basic installation and operation

Reasons for rapid wear of conductive slip rings
![Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev](/img/42/d857452ccfeccbbf1ac34f47e47e2e.jpg)
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev

STM32---IIC

Negative pressure generation of buck-boost circuit
![Measurement fitting based on Halcon learning [i] fuse Hdev routine](/img/91/34c92065e797c87d6ce5ea13903993.jpg)
Measurement fitting based on Halcon learning [i] fuse Hdev routine

实例007:copy 将一个列表的数据复制到另一个列表中。

Management and use of DokuWiki

Wifi-802.11 negotiation rate table
随机推荐
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Buildroot system for making raspberry pie cm3
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
STM32 --- serial port communication
Weidongshan Internet of things learning lesson 1
DCDC circuit - function of bootstrap capacitor
Imx6ull bare metal development learning 1-assembly lit LED
Shell script
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
QEMU demo makefile analysis
Briefly talk about the identification protocol of mobile port -bc1.2
Classic application of MOS transistor circuit design (2) - switch circuit design
On boost circuit
Halcon's practice based on shape template matching [1]
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
The firmware of the connected j-link does not support the following memory access
Compilation warning solution sorting in Quartus II
Nb-iot technical summary
Soem EtherCAT source code analysis I (data type definition)