当前位置:网站首页>[three tier architecture and JDBC summary]
[three tier architecture and JDBC summary]
2022-07-05 08:21:00 【Sing the wind tonight】
I'm learning JDBC In the process of , Note that the tool class must correspond to the query field , Even if the query method is general , But when facing different tables, you should also pay attention to whether the declared attributes of the tool class correspond to the attributes used in the query statement , Otherwise, the object information required by the query statement cannot be queried , There will be no correct result .
Another key point is that there may be differences in the character set of the table , In order to query, add, delete and modify data normally , To be with now properties Add a statement description about the character set in the configuration file , As shown below :
url=jdbc:mysql://localhost:3306/homework3?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true
About multiple variable parameters :
In addition to the corresponding tool classes , In order to improve the generality of query methods, variable parameters are introduced 【args……】 To refer to SQL The parameters required by the statement .
When there are variable parameters ,args Equivalent to an array , The underlying system will first determine its length and then create an array of the same length , Then get the parameters and traverse the array
/**
* Inquire about
* @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]);
}// When there are variable parameters ,args Equivalent to an array ,
// The underlying system will first determine its length and then create an array of the same length , Then get the parameters and traverse the array .
resultSet =pre.executeQuery();
// while (resultSet.next()){
// System.out.println(resultSet.getObject(1) + " " + resultSet.getObject(2)); ;
// }
// Query multiple rows of data using iterators 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 Six steps of :
It is roughly divided into six steps , Namely :
① The load driver
② Link to the database
③ Create a through connection SQL Command transmitter Statement( Better avoidance SQL Inject Now use prepareStatement)
④ Use SQL The command sender sends... To the database SQL command .
⑤ Processing results
⑥ close resource
The relationship between the three layers of the structure is as follows :
边栏推荐
- Stm32--- systick timer
- Makefile application
- Stablq of linked list
- [trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
- NTC thermistor application - temperature measurement
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- Sql Server的存儲過程詳解
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- OC and OD gate circuit
- List of linked lists
猜你喜欢
STM32---ADC
Simple design description of MIC circuit of ECM mobile phone
OC and OD gate circuit
Arduino uses nrf24l01+ communication
Semiconductor devices (I) PN junction
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Halcon's practice based on shape template matching [1]
Drive LED -- GPIO control
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
随机推荐
关于线性稳压器的五个设计细节
Imx6ull bare metal development learning 1-assembly lit LED
实例007:copy 将一个列表的数据复制到另一个列表中。
MySQL MHA high availability cluster
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Briefly talk about the identification protocol of mobile port -bc1.2
Volatile of C language
H264 (I) i/p/b frame gop/idr/ and other parameters
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
DokuWiki deployment notes
Buildroot system for making raspberry pie cm3
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
STM32---IIC
Basic information commands and functions of kernel development
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Solutions to compilation warnings in Quartus II
Synchronization of QT multithreading
PMSM dead time compensation
STM32 --- serial port communication
QEMU demo makefile analysis