介绍:客户端与mysql的连接
连接步骤:
第0步: 导包
第1步:注册驱动 (仅仅做一次)
Class.forName("com.mysql.jdbc.Driver");
第二步:创建连接
Connection conn = DriverManaget.getConnection(
"jdbc:mysql://localhost:3306/study?useSSL=false","root","hui0617"
);
第三步:创建sql语句对象
String sql = "sql语句";
Statement stmt = conn.createStatement();
第四步运行语句
ResultSet re = stmt.executeQuery(sql);
第五步释放资源
re.close;
stmt.close;
conn.close;
PreparedStatement
PreparedStatement是预编译的SQL语句对象,sql语句被预编译并保存在对象中, 被封装的sql语句中可以使用动态包含的参数 ? ,在执行的时候,可以为?传递参数,使用PreparedStatement对象执行sql的时候,sql被数据库进行预编译和预解析,然后被放到缓冲区,
package Jdbc;
import java.sql.*;
public class Jdbc_01 {
public static void main(String[] args) {
m1(19,"女",2,"师浩");
}
public static void m1(int sid,String gender,int class_id,String sname){
Connection conn = null;
PreparedStatement prst = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1注册
Class.forName("com.mysql.jdbc.Driver");
// 2建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?useSSL=false","root","hui0617");
// sql语句
String sql = "insert into student values(?,?,?,?)";
// 语句传输对象
prst = conn.prepareStatement(sql);
prst.setInt(1,sid);
prst.setString(2,gender);
prst.setInt(3,class_id);
prst.setString(4,sname);
int i = prst.executeUpdate();
System.out.println("影响了"+i+"条");
String sql1 = "select * from student";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql1);
while(rs.next()){
System.out.print(rs.getInt("sid"));
System.out.print(rs.getString("gender"));
System.out.print(rs.getInt("class_id"));
System.out.println(rs.getString("sname"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (rs!=null){
rs.close();
}
if (stmt!=null){
stmt.close();
}
if (prst!=null){
prst.close();
}
if (conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Batch多语句操作
Batch可以在一次任务中,执行多条数据
使用:
package work;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class jdbc_02 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/study?useSSL=false","root","hui0617");
stmt = conn.createStatement();
stmt.addBatch("insert into test_jdbc values(4,'师浩四号',13.5)");
stmt.addBatch("delete from test_jdbc where name='师浩三号'");
stmt.addBatch("update test_jdbc set money = 15.25 where id = 1");
// 运行sql语句
stmt.executeBatch();
System.out.println("执行完成");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null){
stmt.close();
}
if (conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
事物
什么是事务?
在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)
四大特性:
ACID
原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
sql注入:
所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据 库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。
例子:
package work;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class jdbc_03 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet re = null;
// 用户的输入
String name = "'or '1' = '1";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/study?useSSL=false","root","hui0617");
String sql = "select * from jdbc_test where name = '" + name + "';";
stmt = conn.createStatement();
System.out.println("执行完成");
re = stmt.executeQuery(sql);
while (re.next()){
System.out.println(re.getInt(1) + " " + re.getString(2) + " " + re.getDouble(3));
;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null){
stmt.close();
}
if (conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}