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

边栏推荐
- Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
- Tailq of linked list
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- MySQL之MHA高可用集群
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- Network communication model -- Network OSI tcp/ip layering
- Summary -st2.0 Hall angle estimation
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- Detailed explanation of SQL server stored procedures
- OLED 0.96 inch test
猜你喜欢
![[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl](/img/8a/702019b44c8e60dffbcd898330afcb.png)
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl

【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
![[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc](/img/9e/2524cbb9b90135c54669ba8d5338b7.jpg)
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc

Simple design description of MIC circuit of ECM mobile phone

FIO测试硬盘性能参数和实例详细总结(附源码)
![C WinForm [display real-time time in the status bar] - practical exercise 1](/img/9f/d193cbb488542cc4c439efd79c4963.jpg)
C WinForm [display real-time time in the status bar] - practical exercise 1

Drive LED -- GPIO control

After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

STM32 tutorial triple ADC interleaved sampling
随机推荐
Verilog -- state machine coding method
Tailq of linked list
Semiconductor devices (III) FET
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Live555 push RTSP audio and video stream summary (I) cross compilation
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
Basic information commands and functions of kernel development
Introduction of air gap, etc
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
STM32 outputs 1PPS with adjustable phase
Hardware and software solution of FPGA key chattering elimination
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Solutions to compilation warnings in Quartus II
Carrier period, electrical speed, carrier period variation
Nb-iot technical summary
Why is 1900 not a leap year
MySQL MHA high availability cluster
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Makefile application
Network port usage