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

边栏推荐
- STM32 single chip microcomputer -- volatile keyword
- Reasons for rapid wear of conductive slip rings
- DokuWiki deployment notes
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- C WinForm [change the position of the form after running] - Practical Exercise 4
- STM32---IIC
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
- Network communication process
- matlab timeserise
- Naming rules for FreeRTOS
猜你喜欢

实例009:暂停一秒输出

STM32 --- serial port communication

How to select conductive slip ring

Nb-iot technical summary

99 multiplication table (C language)

实例010:给人看的时间

Basic embedded concepts

MySQL MHA high availability cluster

Relationship between line voltage and phase voltage, line current and phase current

Communication standard -- communication protocol
随机推荐
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Network communication process
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Step motor generates S-curve upper computer
Bluetooth hc-05 pairing process and precautions
动力电池UL2580测试项目包括哪些
NTC thermistor application - temperature measurement
Why is 1900 not a leap year
VESC Benjamin test motor parameters
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Shell script basic syntax
Compilation warning solution sorting in Quartus II
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
UE像素流,来颗“减肥药”吧!
Basic embedded concepts
Imx6ull bare metal development learning 2- use C language to light LED indicator
实例010:给人看的时间
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Management and use of DokuWiki
Basic information commands and functions of kernel development