当前位置:网站首页>JDBC API details
JDBC API details
2022-07-27 05:25:00 【New an object_】
DriverManager
●DriverManager( Driver management ) effect :
1. Registration drive 
2. Get database connection 
Connection
●Connection( Database connection object ) effect :
1. Access to perform SQL The object of
2. Manage affairs 
import java.sql.*;
public class transaction {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
String sql1="update offer set money=money+500 where Name=' Zhang San '";
String sql2="update offer set money=money-500 where Name=' Li Si '";
Statement sta=conn.createStatement();
try {
conn.setAutoCommit(false);
int count1=sta.executeUpdate(sql1);
System.out.println(" Currently affected rows :"+count1);
int count2=sta.executeUpdate(sql2);
System.out.println(" Currently affected rows :"+count2);
conn.commit();
} catch (SQLException throwables) {
conn.rollback();
throwables.printStackTrace();
}
sta.close();
conn.close();
}
}
Statement
●Statement effect :
1. perform SQL sentence 
ResultSet
●ResultSet( Result set object ) effect :
1. Encapsulates the DQL The result of the query statement 
● Use steps :
●ResultSet Case study :
demand : Inquire about account Account table data , Encapsulated in the Account In the object , And store it in ArrayList Collection 
PreparedStatement
●PreparedStatement effect :
1. precompile SQL Statement and execute : The prevention of SQL Inject problem 
●SQL Inject
*SQL Injection is to modify pre-defined by operating input SQL sentence , The method used to execute code to attack the server .
eg:( Fill in the user name of a login page casually , Password write a special script to login successfully . Such a system is very unsafe , You can attack after logging in , You can do whatever you want in the system )
import java.sql.*;
import java.util.Scanner;
public class JDBCdemo {
public static void main(String[] args) throws Exception {
//1. Registration drive
Class.forName("com.mysql.cj.jdbc.Driver");
//2. Get the connection
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
//3. Accept user input
Scanner in=new Scanner(System.in);
System.out.println(" Please enter a user name ");
String name=in.next();
System.out.println(" Please input a password ");
String password=in.next();
String sql="select * from user where u_name=? and u_password=?";
//4. obtain Statement object
PreparedStatement sta=conn.prepareStatement(sql);
sta.setString(1,name);
sta.setString(2,password);
//5. perform SQL obtain ResultSet Object result set
ResultSet re=sta.executeQuery();
if(re.next()){
System.out.println(" Login successful ");
}else{
System.out.println(" Login failed ");
}
re.close();
sta.close();
conn.close();
}
}

How to solve SQL Injection problem ?:( adopt PreparedStatement Object to solve )
*PreparedStatement principle 
边栏推荐
- Raspberry pie RTMP streaming local camera image
- JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器
- Could not autowire. No beans of ‘userMapper‘ type found.
- mq常见问题
- Database design - relational data theory (ultra detailed)
- Detailed description of polymorphism
- B1027 打印沙漏
- I've heard the most self disciplined sentence: those I can't say are silent
- Differences and examples between internal classes and static internal classes
- LeetCode之268.Missing number
猜你喜欢

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制

JVM Part 1: memory and garbage collection part 11 -- execution engine

JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads

Derivation and explanation of PBR physical illumination calculation formula

JVM上篇:内存与垃圾回收篇二--类加载子系统

Raspberry pie RTMP streaming local camera image
![[CSAPP] Application of bit vectors | encoding and byte ordering](/img/96/344936abad90ea156533ff49e74f59.gif)
[CSAPP] Application of bit vectors | encoding and byte ordering

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

实用小工具: Kotlin 代码片段

Shell course summary
随机推荐
2022年郑州轻工业新生赛题目-打死我也不说
idea远程调试debug
pytorch 数据类型 和 numpy 数据 相互转化
JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器
During its low-level period, this slave edge causes the instruction number to make a corresponding model
The provision of operation and maintenance manager is significantly affected, and, for example, it is like an eep command
文件处理(IO)
Solution and principle analysis of feign call missing request header
How idea creates a groovy project (explain in detail with pictures and texts)
DBUtils
B1026 程序运行时间
Typescript details
File processing (IO)
MQ set expiration time, priority, dead letter queue, delay queue
redis锁
B1026 program running time
Enumeration class implements singleton mode
李宏毅机器学习组队学习打卡活动day06---卷积神经网络
Summary of knowledge points (I)
2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed