1. 程式人生 > >Java JDBC資料庫 之 事務提交

Java JDBC資料庫 之 事務提交

下載 Oracle 或者 Mysql Jar 驅動 

mysql-connector-java-3.0.10-stable-bin.jar 
ojdbc14.jar 

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil
{
	private Connection conn;//資料庫的連結物件
	
	public DBUtil()
	{
		//載入驅動
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	
	//開啟資料的連結
	public Connection openConnection()
	{
		try
		{
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "myspace", "myspace123");
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return conn;
	}
	
	//執行 INSERT DELETE UPDATE 的方法
	public boolean executeUpdate(String sql)
	{
		//開啟資料庫
		openConnection();
		//建立執行 sql 的物件
		try
		{
			Statement stmt=conn.createStatement();
			//執行sql 語句
			if(stmt.executeUpdate(sql)>0)
			{
				return true;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DBClose();
		}
		
		return false;
	}
	
	//執行 sql 的 查詢 SELECT 的方法
	public ResultSet executeQuery(String sql)
	{
		//開啟資料庫連結
		openConnection();
		//建立執行sql 的物件
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return rs;
		
	}
	
	//繫結的sql INSERT DELETE UPDATE 方法
	public boolean executeUpdate(String sql,Object[] params)
	{
		//開啟資料庫
		openConnection();
		//建立繫結物件
		try
		{
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			//填充 ? 的值
			for(int i=0;params!=null&&i<params.length;i++)
			{
				if(params[i] instanceof Integer)
				{
					pstmt.setInt(i+1, Integer.parseInt(params[i].toString()));
					continue;
				}
				if(params[i] instanceof Float)
				{
					pstmt.setFloat(i+1, Float.parseFloat(params[i].toString()));
					continue;
				}
				if(params[i] instanceof Double)
				{
					pstmt.setDouble(i+1, Double.parseDouble(params[i].toString()));
					continue;
				}
				if(params[i] instanceof String)
				{
					pstmt.setString(i+1, params[i].toString());
				}
				
				//java.sql.Date,   java.util.Date
				if(params[i] instanceof java.util.Date)
				{
					//將 java.util.Date 轉成  java.sql.Date
					java.util.Date javaDate=(java.util.Date)params[i];
					java.sql.Date sqlDate=new java.sql.Date(javaDate.getTime());
					pstmt.setDate(i+1, sqlDate);
					continue;
				}

				if(params[i] instanceof java.util.Timestamp)
				{
					//將 java.util.Date 轉成  java.sql.Date
					java.util.Date javaDate=(java.util.Date)params[i];
					java.sql.Timestamp timestamp=new java.sql.Timestamp(javaDate.getTime());
					pstmt.setTimestamp(i+1, timestamp);
					continue;
				}
			}
			//執行 繫結的 sql 語句
			if(pstmt.executeUpdate()>0)
			{
				return true;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DBClose();
		}
		
		return false;
	}
	
	//繫結的sql 查詢 SELECT 方法
		public ResultSet executeQuery(String sql,Object[] params)
		{
			//開啟資料庫
			openConnection();
			//建立繫結物件
			try
			{
				PreparedStatement pstmt=conn.prepareStatement(sql);
				
				//填充 ? 的值
				for(int i=0;params!=null&&i<params.length;i++)
				{
					if(params[i] instanceof Integer)
					{
						pstmt.setInt(i+1, Integer.parseInt(params[i].toString()));
						continue;
					}
					if(params[i] instanceof Float)
					{
						pstmt.setFloat(i+1, Float.parseFloat(params[i].toString()));
						continue;
					}
					if(params[i] instanceof Double)
					{
						pstmt.setDouble(i+1, Double.parseDouble(params[i].toString()));
						continue;
					}
					if(params[i] instanceof String)
					{
						pstmt.setString(i+1, params[i].toString());
					}
					
					//java.sql.Date,   java.util.Date
					if(params[i] instanceof java.util.Date)
					{
						//將 java.util.Date 轉成  java.sql.Date
						java.util.Date javaDate=(java.util.Date)params[i];
						java.sql.Date sqlDate=new java.sql.Date(javaDate.getTime());
						pstmt.setDate(i+1, sqlDate);
						continue;
					}

					if(params[i] instanceof java.util.Timestamp)
					{
						//將 java.util.Date 轉成  java.sql.Date
						java.util.Date javaDate=(java.util.Date)params[i];
						java.sql.Timestamp timestamp=new java.sql.Timestamp(javaDate.getTime());
						pstmt.setTimestamp(i+1, timestamp);
						continue;
					}
				}
				//執行 繫結的 sql 語句
				return pstmt.executeQuery();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			
			
			return null;
		}
	
	//關閉資料庫連結
	public void DBClose()
	{
		if(conn!=null)
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}





package com.transaction;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.db.DBUtil;

public class TestAddMore
{
	public static void main(String[] args)
	{
		DBUtil db=new DBUtil();
		Connection conn=db.openConnection();
		
		try
		{
			//設定事物為不自動提交
			conn.setAutoCommit(false);
			//插入多條記錄
			String sql="";
			for(int i=0;i<10;i++)
			{
				if(i==5) //插入錯誤的sql 語句
				{
					sql="INSERT INTO dept(id,name)VALUES('a','bbbb')";
					PreparedStatement pstmt=conn.prepareStatement(sql);
					pstmt.executeUpdate();
				}
				else
				{
					sql="INSERT INTO dept(id,name)VALUES(?,?)";
					PreparedStatement pstmt=conn.prepareStatement(sql);
					pstmt.setInt(1,i+1);
					pstmt.setString(2, "部門"+i);
					pstmt.executeUpdate();
				}
				
			}
			
			//事物提交
			conn.commit();
		} catch (SQLException e)
		{
			//回滾
			try
			{
				conn.rollback();
			} catch (SQLException e1)
			{
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		finally
		{
			db.DBClose();
			
		}
		
		
	}
}


http://www.verejava.com/?id=16998493339542