当前位置:网站首页>[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 :
边栏推荐
- 动力电池UL2580测试项目包括哪些
- Synchronization of QT multithreading
- MHA High available Cluster for MySQL
- Installation and use of libjpeg and ligpng
- Why is 1900 not a leap year
- Hardware and software solution of FPGA key chattering elimination
- Explication de la procédure stockée pour SQL Server
- Communication standard -- communication protocol
- MySQL MHA high availability cluster
- Charge pump boost principle - this article will give you a simple understanding
猜你喜欢
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
Volatile of C language
Measurement fitting based on Halcon learning [i] fuse Hdev routine
MySQL之MHA高可用集群
C WinForm [change the position of the form after running] - Practical Exercise 4
Step motor generates S-curve upper computer
STM32 --- NVIC interrupt
C WinForm [display real-time time in the status bar] - practical exercise 1
Array integration initialization (C language)
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
随机推荐
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
动力电池UL2580测试项目包括哪些
Count the number of inputs (C language)
List of linked lists
Arduino uses nrf24l01+ communication
Carrier period, electrical speed, carrier period variation
go依赖注入--google开源库wire
Stm32--- systick timer
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Talk about the circuit use of TVs tube
General makefile (I) single C language compilation template
STM32 tutorial triple ADC interleaved sampling
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
FIO测试硬盘性能参数和实例详细总结(附源码)
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
Management and use of DokuWiki (supplementary)
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Halcon's practice based on shape template matching [2]
实例009:暂停一秒输出
My-basic application 1: introduction to my-basic parser