当前位置:网站首页>【三层架构及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命令。
⑤处理结果
⑥关闭资源
三层结构各层之间的关系如下:

边栏推荐
- Shell script basic syntax
- Explain task scheduling based on Cortex-M3 in detail (Part 1)
- Reasons for rapid wear of conductive slip rings
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- STM32 --- NVIC interrupt
- Detailed explanation of SQL server stored procedures
- 实例009:暂停一秒输出
- Anonymous structure in C language
- On boost circuit
- 实例006:斐波那契数列
猜你喜欢
![C WinForm [help interface - send email] - practice five](/img/2a/c4e7abe054e6fdd45acc7d297a033d.jpg)
C WinForm [help interface - send email] - practice five

Network communication process
![Measurement fitting based on Halcon learning [i] fuse Hdev routine](/img/91/34c92065e797c87d6ce5ea13903993.jpg)
Measurement fitting based on Halcon learning [i] fuse Hdev routine

Installation and use of libjpeg and ligpng
![[paper reading] the latest transfer ability in deep learning: a survey in 2022](/img/6b/b564fb7a6895329073fb5eaff64340.png)
[paper reading] the latest transfer ability in deep learning: a survey in 2022

Some thoughts on extracting perspectives from ealfa and Ebeta

MySQL MHA high availability cluster

VESC Benjamin test motor parameters

STM32---ADC

实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
随机推荐
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
How to copy formatted notepad++ text?
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
OLED 0.96 inch test
STM32 single chip microcomputer - external interrupt
Basic information commands and functions of kernel development
STM32 outputs 1PPS with adjustable phase
Keil use details -- magic wand
What are the test items of power battery ul2580
List of linked lists
Negative pressure generation of buck-boost circuit
Vofa+ software usage record
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Count the number of inputs (C language)
My-basic application 2: my-basic installation and operation
Hardware 1 -- relationship between gain and magnification
STM32---ADC
Connection mode - bridge and net
Why is 1900 not a leap year