1.实践PreparedStament的CRUD操作。
2.对比Statement和PreparedStatement的大批量操作耗时?(100000记录)
3.事务特点?
4.mysql client如何控制事务?
-------------------------------------------
实践PreparedStament的CRUD操作。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.junit.Test;
/**
* 使用PreparedStatement
*/
public class TestJDBC3 {
@Test
public void insert(){
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
//3.创建语句对象
String sql = "insert into stus(id,name,age) values(?,?,?)" ;
PreparedStatement ppst = conn.prepareStatement(sql);
ppst.setInt(1, 100);
ppst.setString(2, "tomas");
ppst.setInt(3, 23);
ppst.execute();
ppst.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* update , id = 1, age = 10
*/
@Test
public void update() {
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
//3.创建语句对象
Statement st = conn.createStatement();
String sql= "update stus set age = ? where id = ?" ;
PreparedStatement ppst = conn.prepareStatement(sql);
ppst.setInt(1, 10);
ppst.setInt(2, 1);
boolean b = ppst.execute();
System.out.println("" + b);
st.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* delete , id = 1
*/
@Test
public void delete() {
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
//3.创建语句对象
String sql = "delete from stus where id = ?" ;
PreparedStatement ppst = conn.prepareStatement(sql);
ppst.setInt(1, 1);
boolean b = ppst.execute();
System.out.println("" + b);
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void select() {
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
//3.创建语句对象
//select id, name ,age form ...
String sql = "select * from stus where id > ?" ;
PreparedStatement ppst = conn.prepareStatement(sql);
ppst.setInt(1, 1);
boolean b = ppst.execute();
//判断是否是查询
if(b){
//获取查询结果集
ResultSet rs = ppst.getResultSet();
//移动游标
while(rs.next()){
//int id = rs.getInt("id");
Integer id = (Integer) rs.getObject("id");
String name = rs.getString("name");
Integer age = (Integer) rs.getObject("age");
System.out.println("id=" + id + ",name=" + name + ",age=" + age);
}
//结果集元数据
ResultSetMetaData meta = rs.getMetaData();
for(int i = 0 ; i < meta.getColumnCount() ; i ++){
System.out.print(meta.getColumnLabel(i + 1) + " ");
}
}
//
//5.释放资源
ppst.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
2.对比Statement和PreparedStatement的大批量操作耗时?(100000记录)
//Statement
package com.it18zhang.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.junit.Test;
public class TestJDBC {
/**
* insert1
*/
@Test
public void insert1(){
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
//设置自动提交模式
conn.setAutoCommit(false);
//3.创建语句对象
Statement st = conn.createStatement();
String sql = "insert into stus(id,name,age) values(9,‘tomson‘,11)" ;
long start = System.currentTimeMillis() ;
//4.执行语句
for(int i = 0 ; i < 100000 ; i ++){
sql = "insert into stus(id,name,age) values("+(i + 1)+",‘tom"+i+"‘,12)" ;
boolean b = st.execute(sql);
}
conn.commit();
System.out.println(System.currentTimeMillis() - start);
//5.释放资源
st.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
//PreparedStatement 程序
package com.it18zhang.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.junit.Test;
/**
* 使用PreparedStatement
*/
public class TestJDBC3 {
@Test
public void insert(){
try {
//1.注册驱动
String driver = "com.mysql.jdbc.Driver" ;
Class.forName(driver);
//2.建立连接
String url = "jdbc:mysql://localhost:3306/mybase" ;
String username = "root" ;
String password = "root" ;
Connection conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
//3.创建语句对象
String sql = "insert into stus(id,name,age,password) values(?,?,?,?)" ;
PreparedStatement ppst = conn.prepareStatement(sql);
for(int i = 0 ; i < 100000 ; i ++){
ppst.setInt(1, i + 1);
ppst.setString(2, "tomas" + i);
ppst.setInt(3, 10);
ppst.setString(4, "123");
ppst.addBatch();
if(i % 10000 == 0){
ppst.executeBatch();
ppst.clearBatch();
}
}
ppst.executeBatch();
conn.commit();
System.out.println("耗时 : " + (System.currentTimeMillis() - start));
ppst.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
3.事务特点?
原子性(atomicity):组成事务处理的语句形成了一个逻辑单元,不能只执行其中的一部分。
一致性(consistency):在事务处理执行前后,数据库是一致的(两个账户要么都变,或者都不变)。
隔离性(isolcation):一个事务处理对另一个事务处理没有影响。
持续性(durability):事务处理的效果能够被永久保存下来 。
acid
a: atomic,原子性,不可分割。全成功、全失败。
c: consistent,一致性
i: isolate,隔离性
d: durable,永久性。
4.mysql client如何控制事务?
1.关闭自动提交
set autocommit=0;
2.开启事务
start transaction;
3.提交事务
commit;
4.回滚
rollback;
本文出自 “作业” 博客,请务必保留此出处http://10718270.blog.51cto.com/10708270/1792593
原文地址:http://10718270.blog.51cto.com/10708270/1792593