当前位置:网站首页>[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---ADC
- Anonymous structure in C language
- STM32 single chip microcomputer -- volatile keyword
- Imx6ull bare metal development learning 1-assembly lit LED
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- NTC thermistor application - temperature measurement
- Nb-iot technical summary
猜你喜欢
Hardware 1 -- relationship between gain and magnification
Array integration initialization (C language)
Stablq of linked list
MHA High available Cluster for MySQL
STM32---IIC
Makefile application
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Measurement fitting based on Halcon learning [i] fuse Hdev routine
随机推荐
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Live555 push RTSP audio and video stream summary (I) cross compilation
Bootloader implementation of PIC MCU
Imx6ull bare metal development learning 2- use C language to light LED indicator
Several important parameters of LDO circuit design and type selection
STM32 single chip microcomputer - external interrupt
STM32 --- configuration of external interrupt
C WinForm [help interface - send email] - practice five
动力电池UL2580测试项目包括哪些
Basic embedded concepts
Buildroot system for making raspberry pie cm3
Briefly talk about the identification protocol of mobile port -bc1.2
Vofa+ software usage record
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Reasons for rapid wear of conductive slip rings
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Talk about the circuit use of TVs tube
Connection mode - bridge and net
STM32 single chip microcomputer -- volatile keyword