1. 程式人生 > >JDBC事務、批處理、大物件的基本使用

JDBC事務、批處理、大物件的基本使用

一、測試事務的概念和用法 

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();
			}
		}
	}
}