当前位置:网站首页>JDBC API 详解
JDBC API 详解
2022-07-27 05:01:00 【new一个对象_】
DriverManager
●DriverManager(驱动管理类)作用:
1.注册驱动
2.获取数据库连接
Connection
●Connection(数据库连接对象)作用:
1.获取执行SQL的对象
2.管理事务
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='张三'";
String sql2="update offer set money=money-500 where Name='李四'";
Statement sta=conn.createStatement();
try {
conn.setAutoCommit(false);
int count1=sta.executeUpdate(sql1);
System.out.println("当前受影响的行:"+count1);
int count2=sta.executeUpdate(sql2);
System.out.println("当前受影响的行:"+count2);
conn.commit();
} catch (SQLException throwables) {
conn.rollback();
throwables.printStackTrace();
}
sta.close();
conn.close();
}
}
Statement
●Statement作用:
1.执行SQL语句
ResultSet
●ResultSet(结果集对象)作用:
1.封装了DQL查询语句的结果
●使用步骤:
●ResultSet案例:
需求:查询account账户表数据,封装为Account对象中,并且存储到ArrayList集合中
PreparedStatement
●PreparedStatement作用:
1.预编译SQL语句并执行:预防SQL注入问题
●SQL注入
*SQL注入是通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。
eg:(某登录页面用户名随便填写,密码写一段特殊的脚本便可登录成功。这样的系统非常不安全,登录后可进行攻击,可在系统里为所欲为)
import java.sql.*;
import java.util.Scanner;
public class JDBCdemo {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
//3.接受用户输入
Scanner in=new Scanner(System.in);
System.out.println("请输入用户名");
String name=in.next();
System.out.println("请输入密码");
String password=in.next();
String sql="select * from user where u_name=? and u_password=?";
//4.获取Statement对象
PreparedStatement sta=conn.prepareStatement(sql);
sta.setString(1,name);
sta.setString(2,password);
//5.执行SQL获取ResultSet 对象结果集
ResultSet re=sta.executeQuery();
if(re.next()){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
re.close();
sta.close();
conn.close();
}
}

如何解决SQL注入问题?:(通过PreparedStatement对象来解决)
*PreparedStatement原理
边栏推荐
- Acceptance and neglect of events
- JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
- ssm框架整合
- Static and final keyword learning demo exercise
- JVM Part 1: memory and garbage collection part 11 -- execution engine
- 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
- 深入 Qt5 信号槽新语法
- 牛客剑指offer--JZ12 矩阵中的路径
- Could not autowire. No beans of ‘userMapper‘ type found.
- How idea creates a groovy project (explain in detail with pictures and texts)
猜你喜欢

《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译

File processing (IO)

JVM Part 1: memory and garbage collection part 12 -- stringtable

JVM上篇:内存与垃圾回收篇十一--执行引擎

素数筛选(埃氏筛法,区间筛法,欧拉筛法)

2、 MySQL advanced

老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破

pyside2____ 1. Installation and case listing

Another skill is to earn 30000 yuan a month+

1、 MySQL Foundation
随机推荐
Standard dialog qmessagebox
Use of collection framework
feign调用丢失请求头问题解决及原理分析
How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
ERROR! MySQL is not running, but PID file exists
34. Analyze flexible.js
Machine learning overview
How to create an applet project
File processing (IO)
Invert a Binary Tree
标准对话框 QMessageBox
How idea creates a groovy project (explain in detail with pictures and texts)
Demo of throttling function -- regular expression matching
Installation and template setting of integrated development environment pychar
JVM上篇:内存与垃圾回收篇十四--垃圾回收器
Could not autowire.No beans of ‘userMapper‘ type found.
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
How to store the startprocessinstancebykey method in acticiti in the variable table
微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”
Acticiti中startProcessInstanceByKey方法在variable表中的如何存储