JDBC代码简化优化——(二)查询篇

实现查询,初始思路

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

public class Select {

	public static void main(String[] args) {
		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();
			String sql = "select * from user_info";
			resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				String id = resultSet.getString("id");
				String userName = resultSet.getString("user_name");
				String password = resultSet.getString("password");
				System.out.println(id+","+userName+","+password);
			}
		} 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();
			}
		}
	}
}

下面来提高代码的复用

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

public class Select {

	public static ResultSet query(String sql) {
		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();
			return statement.executeQuery(sql);
		} 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();
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		String sql = "select * from user_info";
		try {
			ResultSet resultSet = query(sql);
			while(resultSet.next()) {
				String id = resultSet.getString("id");
				String userName = resultSet.getString("user_name");
				String password = resultSet.getString("password");
				System.out.println(id+","+userName+","+password);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

第一次尝试,通过封装一个调用了executeQuery方法的query方法,使之返回一个ResultSet对象,主方法中ResultSet resultSet = query(sql);程序执行发现报错,原因是当执行代码ResultSet resultSet = query(sql)时,调用了 query时会执行finally资源释放,接口对象关闭

不做过多赘述,最终代码如下
需要自行创建的接口如下

interface IRowMapper{
	void rowMapper(ResultSet res) ;
}
//引入设计模式————命令模式
public static void query(String sql,IRowMapper rowMapper) {
		/*调用query方法,势必创建一个IRowMapper接口的实现类对象
		*/
		Connection con = null;
		Statement sta = null;
		ResultSet res = null;  //executeQuery方法会返回一个ResultSet对象
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			sta = con.createStatement();
			res= sta.executeQuery(sql);
			rowMapper.rowMapper(res);//多态
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (res!=null) {
					res.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (sta!=null) {
					sta.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (con!=null) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

public static void main(String[] args) {    //主方法
		String sql = "select * from user_info";		
		class RowMapper implements IRowMapper{   //内部类
			@Override
			public void rowMapper(ResultSet rs) {   
				try {
					while(rs.next()) {
						String id = rs.getString("id");
						String userName = rs.getString("user_name");
						String password = rs.getString("password");
						System.out.println(id+","+userName+","+password);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}		
		RowMapper rowMapper = new RowMapper();
		query(sql,rowMapper);
		/*代码自上而下走,当走到这一行时,query()方法中传入内部类对象,准确的说是内部类的地址
		*query(String sql,IRowMapper rowMapper)中rowMapper传入了地址
		*然后会执行try中的rowMapper.rowMapper(res)时,rowMapper表面调用的接口的抽象方法,
		*实质调用变量地址即对象对应的抽象方法,也即多态,执行权又回到了内部类中的代码
		*rowMapper.rowMapper(res)执行完以后又回到了query()方法中try-catch部分,
		*然后执行finally就完成了资源的释放*/
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值