模仿DBUtils, 完成增删改的功能
public class MyQueryRunner {
private DataSource dataSource;
public MyQueryRunner(DataSource dataSource) {
this.dataSource = dataSource;
}
public int update(String sql,Object... params) throws SQLException {
//执行sql语句
//1. 获得连接
Connection conn = this.prepareConnection();
//2. 预编译sql语句
PreparedStatement pstm = this.prepareSql(sql, conn);
//3. 填充占位符处的参数
this.fillStatement(pstm,params);
//4. 执行sql语句
int rows = pstm.executeUpdate();
//5. 关闭资源
pstm.close();
conn.close();
return rows;
}
private void fillStatement(PreparedStatement pstm, Object... params) throws SQLException {
//1. 判断sql语句的参数个数和传入的参数个数是否一致
//获取参数元数据
ParameterMetaData parameterMetaData = pstm.getParameterMetaData();
//使用参数元数据获取sql语句的参数个数
int parameterCount = parameterMetaData.getParameterCo