版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/84317013
一、测试事务的概念和用法
package com.chenfu.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试事务的概念和用法
* @author Administrator
*
*/
public class Demo06 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
conn.setAutoCommit(false);//JDBC默认自动提交
ps1 = conn.prepareStatement("insert into t_use (username,pwd) values(?,?)");
ps1.setObject(1, "chenfu");
ps1.setObject(2, "123456");
ps1.execute();
System.out.println("插入一个用户,chenfu");
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ps2 = conn.prepareStatement("insert into t_user (username,pwd) values(?,?)");
ps2.setObject(1, "xvbolai");
ps2.setObject(2, "123456");
ps2.execute();
System.out.println("第二个用户,徐伯莱");
conn.commit();
} catch (ClassNotFoundException e) {
e.printStackTrace();
try {
conn.rollback();//回滚
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{//resultSet-->statment-->connection这样的顺序
try {
if(ps1!=null){
ps1.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
二、测试批量处理的基本用法
package com.chenfu.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试批量处理的基本用法
* @author Administrator
*
*/
public class Demo05 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
conn.setAutoCommit(false);//设为手动提交!
long start = System.currentTimeMillis();
stmt = conn.createStatement();
for(int i = 0; i < 2900; i ++){
stmt.addBatch("insert into t_user (username,psw,regtime) values('gao"+i+"','666666',now())");
}
stmt.executeBatch();
conn.commit();
long end = System.currentTimeMillis();
System.out.println("插入29000数据耗时"+(end-start));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{//resultSet-->statment-->connection这样的顺序
try {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stmt!=null){
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、大对象的使用
package com.chenfu.test;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 测试CLOB 文本大对象的使用
* 包含:将字符串、文件内容插入数据库中CLOB字段、将CLOB字段取出来
* @author Administrator
*
*/
public class Demo09 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Reader r =null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
ps = conn.prepareStatement("insert into t_user (username,myInfo) values (?,?)");
ps.setString(1, "高旗");
// ps.setClob(2, new FileReader(new File("d:/a.txt")));//将文本文件内容直接输入到数据库中
//将程序中的字符串输入到数据库CLOB的字段中
// ps.setClob(2, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaaaa".getBytes()))));
ps = conn.prepareStatement("select * from t_user where id=?");
ps.setObject(1, 4);
rs = ps.executeQuery();
while(rs.next()){
Clob c = rs.getClob("myInfo");
r = c.getCharacterStream();
int temp = 0 ;
while((temp=r.read())!=-1){
System.out.println((char)temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{//resultSet-->statment-->connection这样的顺序
try {
if(r!=null){
r.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.chenfu.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 测试BLOB 二进制大对象的使用
* @author Administrator
*
*/
public class Demo10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is =null;
OutputStream os = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
// ps = conn.prepareStatement("insert into t_user (username,headImg) values (?,?)");
// ps.setString(1, "高旗");
// ps.setBlob(2, new FileInputStream("d:/icon.jpg"));
// ps.execute();
ps = conn.prepareStatement("select * from t_user where id=?");
ps.setObject(1, 8);
rs = ps.executeQuery();
while(rs.next()){
Blob b = rs.getBlob("headImg");
is = b.getBinaryStream();
os = new FileOutputStream("d:/a.jpg");
int temp = 0 ;
while((temp=is.read())!=-1){
os.write(temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{//resultSet-->statment-->connection这样的顺序
try {
if(is!=null){
is.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}