BaseDao中直接跳过向服务器发送SQL语句的这几行 都有哪些可能
package com.bdqn.shop.tools;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BaseDao {
protected Connection connection;
private ResultSet rs;
protected Connection getConnection(){
try {
Context cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:comp/env/jdbc/shop");
//获取连接
connection= ds.getConnection();
} catch (Exception e) {
}
return this.connection;
}
/**
* 执行select语句
* @param sql select语句
* @param params 参数的值
* @return
*/
public ResultSet getResult(String sql,Object[] params){
try {
this.getConnection();
//连接创建PreparedStatement对象
PreparedStatement smt = connection.prepareStatement(sql);
if(params!=null){
for (int i = 0; i <params.length; i++) {
smt.setObject(i+1, params[i]);
}
}
//向服务器发送SQL语句,并返回查询的结果集
this.rs = smt.executeQuery();
} catch (Exception e) {
// TODO: handle exception
}
return rs;
}
/**
* 执行insert update delete语句
* @param insert update delete语句
* @param params 参数
* @return 是否执行成功
*/
public boolean saveResult(String sql,Object[] params){
boolean flag=false;
try {
this.getConnection();
//连接创建PreparedStatement对象
PreparedStatement smt = connection.prepareStatement(sql);
if(params!=null){
for (int i = 0; i <params.length; i++) {
smt.setObject(i+1, params[i]);
}
}
//向服务器发送SQL语句,返回受影响的行数
int row = smt.executeUpdate();
if(row==1){
flag=true;
}
} catch (Exception e) {
e.printStackTrace();
flag=false;
}finally{
this.closeResource();
}
return flag;
}
public void closeResource(){
try {
if (this.rs != null) {
rs.close();
}
if(this.connection!=null){
connection.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}