DBUtil工具類

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 Jerry
	 */
		
    public class DBUtil2 {

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");//加載驅動
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
		
	/**
	 * 獲取數據庫鏈接
	 * 
	 * @author Jerry
	 */
	private static Connection connect(){
		String url=PropertiesUtil.getValue("jdbc.url");
		String userName=PropertiesUtil.getValue("jdbc.userName");
		String password=PropertiesUtil.getValue("jdbc.password");
			
		try {
			return DriverManager.getConnection(url,userName,password);//創建鏈接
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
		
	/**
	 * 修改數據
	 * 
	 * @author Jerry
	 */
	 public static boolean upDate(String sql) {
		Connection connection =null;
		Statement statement = null;
		connect();
		try {
			connection=connect();
			statement = connection.createStatement();
			int result = statement.executeUpdate(sql);
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(statement,connection);
		}
		return false;
	}
		
	/**
	 * 修改數據
	 * 
	 * @author Jerry
	 */
	 public static boolean upDate(String sql,Object ... params) {
		Connection connection =null;
		Statement statement = null;
		try {
			connection=connect();
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= params.length; i++) {
				preparedStatement.setObject(i,params[i-1] );
			}
			int effectRows = preparedStatement.executeUpdate();
			return effectRows > 0;
		} catch (Exception e) {
				e.printStackTrace();
		}finally {
			close(statement,connection);
		}
		return false;
	}
		
	/**
	 * 判斷是否存在
	 * 
	 * @author Jerry
	 */
	 public static boolean exist(String sql) {
		class RowMapper implements IRowMapper{
			boolean state=false;
			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					if(resultSet.next()) {
						state = true;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql,rowMapper);
		return rowMapper.state;
			
	}
		
	/**
	 * 判斷數據庫是否存在
	 *
	 * @author Jerry
	 */
	 public static boolean exist(String sql,Object ... params) {
		class RowMapper implements IRowMapper{
			boolean state=false;
			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
				
		}
		RowMapper rowMapper = new RowMapper();
		select(sql,rowMapper);
		return rowMapper.state;
			
	}
	
	/**
	 * 批處理方法
	 *
	 * @author Jerry
	 */
	public static boolean Batch(String sql,String ... sqls) {
	    Connection connection=null;
	    PreparedStatement preparedStatement=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection=getConnection();
			connection.setAutoCommit(false);//在創建語句之前//set commit =0是事務的開啓,事務不可分
			preparedStatement=connection.prepareStatement(sql);
			for (String s : sqls) {
				preparedStatement.addBatch();
			}
			preparedStatement.executeBatch();
			connection.commit();
				
		} catch (Exception e) {
			e.printStackTrace();
			if(connection!=null) {//重點:事務中存在相同數據,一定要等待上一個事務執行完畢下一個事務才能開始執行
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}finally {
					try {
					close(preparedStatement,connection);
					} catch (Exception e1) {
					e1.printStackTrace();
					}
				}
			}
				
		}
	
		return false;
}

        /**
	 * 批處理方法
	 *
	 * @author Jerry
	 */
	public static boolean Batch(String sqls,Object[]...params) {
	    Connection connection=null;
	    PreparedStatement prepareStatement=null;
	    	try {
			Class.forName("com.mysql.jdbc.Driver");
			connection=getConnection();
			connection.setAutoCommit(false);//在創建語句之前//set commit =0是事務的開啓,事務不可分
			prepareStatement=connection.prepareStatement(sqls);
			for (int i = 0; i < params.length; i++) {
				for(int j=1;j<=params[i].length;j++) {
					prepareStatement.setObject(j, params[i][j-1]);
				}
				prepareStatement.addBatch();
			}
			prepareStatement.executeBatch();
			connection.commit();
				
		} catch (Exception e) {
			e.printStackTrace();
			if(connection!=null) {//重點:事務中存在相同數據,一定要等待上一個事務執行完畢下一個事務才能開始執行
				try {
				    	connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}finally {
					try {
					close(prepareStatement,connection);
					} catch (Exception e1) {
					e1.printStackTrace();
					}
				}
			}
				
		}
	
		return false;
}
	
	/**
	 * 查詢數據
	 *
	 * @author Jerry
	 */
  	 public static void select(String sql,IRowMapper rowMapper) {//參數傳遞
		Connection connection =null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection=connect();
			statement = connection.createStatement();//6、創建語句
			resultSet = statement.executeQuery(sql);//7、執行語句
			rowMapper.rowMapper(resultSet);//8、上轉型對象表面調用接口的抽象方法實則調用main方法中內部類中重寫過的方法
		} catch (Exception e) {
			e.printStackTrace();
		}finally {//10、釋放資源
			close(statement,connection);
		}
	}
	/**
	 * 釋放資源
	 *
	 * @author Jerry
	 */
 	 private static void close(Statement statement,Connection connection) {
		if (statement!=null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		}
		if (connection!=null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章