JDBC代码简化优化——(三)DBUtil工具类的封装(still need to supplement)

代码如下

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

interface IRowMapper{
	void rowMapper(ResultSet rs);
}

public class DBUtil {
	
	public static void query(String sql,IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			rowMapper.rowMapper(resultSet);//多态
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (resultSet!=null) {
					resultSet.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (connection!=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement= null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			statement= connection.createStatement();
			return statement.executeUpdate(sql)>0;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			try {
				if (connection !=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

interface IRowMapper{
	void rowMapper(ResultSet rs);
}

public class DBUtil {
	
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");//1、加载驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private static Connection getConnection() {//2、获取连接
		try {
			return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void query(String sql,IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			statement = connection.createStatement();//3、创建语句
			resultSet = statement.executeQuery(sql);//4、执行SQL
			rowMapper.rowMapper(resultSet);//多态,5、处理结果
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(resultSet,statement,connection);//6、释放资源
		}
	}

	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement= null;
		try {
			connection = getConnection();
			statement= connection.createStatement();//3、创建语句
			return statement.executeUpdate(sql)>0;//4、执行SQL 5、处理结果
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(statement,connection);//6、释放资源
		}
		
		return false;
	}
	
	private static void close(Statement statement,Connection connection) {
		try {
			if (statement!=null) {
				statement.close();
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			if (connection !=null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private static void close(ResultSet resultSet,Statement statement,Connection connection) {
		try {
			if (resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		close(statement,connection);
	}
}

防SQL注入补充后代码如下

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

import org.apache.log4j.Logger;

/**
 * 数据库工具类
 *
 * @author zzs
 */
public class DButil {
	static Connection con =null;
	static Statement sta =null;
	static String sql =null;
	static ResultSet result = null;
	/**
	 * 获取连接
	 *
	 */
	private static Logger logger = Logger.getLogger(DButil.class);
	public static Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = PropertiesUtil.getValue("url");
			String userName = PropertiesUtil.getValue("user_name");
			String password = PropertiesUtil.getValue("password");
			return DriverManager.getConnection(url,userName,password);	
		} catch (Exception e) {
			logger.debug(e.getMessage(),e);
		}
		return null;
	}
	/**
	 * 数据库修改方法
	 *
	 */
	public static boolean upDate(String sql) {
		con = getConnection();	
		try {
			sta = con.createStatement();
			return sta.executeUpdate(sql)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(result, sta, con);
		}
		return false;
	}
	/**
	 * 数据库修改方法,防止SQL注入
	 *
	 */
	public static boolean upDate(String sql,Object...array) {
		con = getConnection();
		PreparedStatement preparedStatement =null;
		try {
			preparedStatement = con.prepareStatement(sql);
			for (int i = 1; i <=array.length; i++) {
				preparedStatement.setObject(i, array[i-1]);
			}
			return preparedStatement.executeUpdate()>0;
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			close(result, preparedStatement, con);
		}
		return false;
	}	
	/**
	 * 数据库查询方法
	 *
	 */
	public static void sel(IRowMapper rowMapper,String sql) {
		try {
			con = getConnection();
			sta = con.createStatement();
			result = sta.executeQuery(sql);
			rowMapper.rowMapper(result);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(result, sta, con);
		}
	}
	/**
	 * 数据库查询方法,防止SQL注入
	 *
	 */
	public static void sel(IRowMapper rowMapper,String sql,Object...array) {
		Scanner sc = new Scanner(System.in);
		try {
			con = getConnection();
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			for (int i = 1; i <=array.length; i++) {
				preparedStatement.setObject(i, array[i-1]);
			}
			result = preparedStatement.executeQuery();
			rowMapper.rowMapper(result);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(result, sta, con);
		}
		
	}
	/**
	 * 资源释放方法
	 *
	 */
	private static void close(Statement statement,Connection connection) {
		try {
			if (statement!=null) {
				statement.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 资源释放方法
	 *
	 */
	private static void close(ResultSet result,Statement statement,Connection connection) {
		try {
			if (result!=null) {
				result.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close(statement,connection);
	}
}

事务的添加

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DCL {
	public static void main(String[] args) {
		Connection con=null;
		Statement sta=null;
		String sql="";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://126.0.0.1:3306/test,root,root");
			con.setAutoCommit(false);
			sta=con.createStatement();
			sta.addBatch("update account set money=money-100 where card_id= '1234567890'");
			sta.addBatch("update account set money=money+100 where card_id= '0987654321'");
			sta.executeBatch();
			con.commit();
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			if(sta!=null) {
				try {
					sta.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(con!=null) {
				try {
					con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}	
		}	
	}
}

你可能感兴趣的:(JDBC)