当前位置:网站首页>【三层架构及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命令。
⑤处理结果
⑥关闭资源
三层结构各层之间的关系如下:
边栏推荐
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- Brief discussion on Buck buck circuit
- UE像素流,来颗“减肥药”吧!
- STM32 single chip microcomputer - bit band operation
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- MySQL之MHA高可用集群
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- go依赖注入--google开源库wire
- Various types of questions judged by prime numbers within 100 (C language)
- Drive LED -- GPIO control
猜你喜欢
Nb-iot technical summary
leetcode - 445. 两数相加 II
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Summary of SIM card circuit knowledge
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Semiconductor devices (III) FET
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
99 multiplication table (C language)
STM32 single chip microcomputer - bit band operation
VESC Benjamin test motor parameters
随机推荐
QEMU demo makefile analysis
Take you to understand the working principle of lithium battery protection board
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
MySQL MHA high availability cluster
Explication de la procédure stockée pour SQL Server
实例008:九九乘法表
STM32 tutorial triple ADC interleaved sampling
Several important parameters of LDO circuit design and type selection
Hardware 1 -- relationship between gain and magnification
Count the number of inputs (C language)
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Some tips for using source insight (solve the problem of selecting all)
FIO测试硬盘性能参数和实例详细总结(附源码)
STM32 outputs 1PPS with adjustable phase
DCDC circuit - function of bootstrap capacitor
STM32---ADC
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Tailq of linked list
My-basic application 1: introduction to my-basic parser
Relationship between line voltage and phase voltage, line current and phase current