当前位置:网站首页>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 
边栏推荐
- JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads
- 34. Analyze flexible.js
- 6 zigzag conversion of leetcode
- B1029 旧键盘
- Niuke sword refers to the path in the offer--jz12 matrix
- mq常见问题
- Differences among left join, inner join and right join
- JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈
- Notes Series docker installation PostgreSQL 14
- JVM上篇:内存与垃圾回收篇二--类加载子系统
猜你喜欢

Database design - relational data theory (ultra detailed)

李宏毅机器学习组队学习打卡活动day06---卷积神经网络

JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈

Differences among left join, inner join and right join

Mysql速成

笔记系列之docker安装Postgresql 14

JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area

SSM framework integration

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

Message reliability processing
随机推荐
LeetCode之268.Missing number
pytorch 数据类型 和 numpy 数据 相互转化
[acwing] solution to the 61st weekly match
李宏毅机器学习组队学习打卡活动day03---误差和梯度下降
2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed
redis事务
The difference between strlen and sizeof
numpy 数据类型转化
Use of collection framework
How to sinicize the JMeter interface?
Summary of knowledge points (I)
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
The receiver sets the concurrency and current limit
B1021 个位数统计
B1024 科学计数法
redis持久化
SQL数据库→约束→设计→多表查询→事务
B1025 反转链表*******
JVM上篇:内存与垃圾回收篇十二--StringTable
Quoted popular explanation