java通用DAO
package org.gaoyoubo.dao;
import java.sql.*;
import java.util.*;
import javax.servlet.jsp.jstl.sql.*;
public class CommanDao {
private Connection con;
private String sql;
private List parameters;
//设置连接
public void setCon(Connection con) {
this.con = con;
}
//设置sql语句;
public void setSql(String sql) {
this.sql = sql;
}
//设置sql语句参数;
public void setParameters(List aparametersrameters) {
this.parameters = aparametersrameters;
}
/**
* 执行查询;
* @return
*/
public Result executeQuery(){
Result result = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
//如果sql语句有参数;
if(parameters != null && parameters.size() > 0){
pstmt = con.prepareStatement(sql);
setParameters(pstmt, parameters);
rs = pstmt.executeQuery();
}
//如果没有参数;
else{
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
//将ResultSet转换成Result;
result = ResultSupport.toResult(rs);
return result;
}
/**
* 执行更新;
* @return 返回受影响行数;
*/
public int executeUpdate(){
int noOfRows = 0;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
//如果参数不为空;
if(parameters != null && parameters.size() > 0){
pstmt = con.prepareStatement(sql);
setParameters(pstmt, parameters);
noOfRows = pstmt.executeUpdate();
}
else{
pstmt = con.prepareStatement(sql);
noOfRows = pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return noOfRows;
}
/**
* 方法,为sql语句设置参数;
* @param pstmt 指令
* @param parameters 参数集合;
* @throws SQLException
*/
private void setParameters(PreparedStatement pstmt,List parameters) throws SQLException{
for (int i = 0; i < parameters.size(); i++) {
Object v = parameters.get(i);
pstmt.setObject(i+1, v);
}
}
}
import java.sql.*;
import java.util.*;
import javax.servlet.jsp.jstl.sql.*;
public class CommanDao {
private Connection con;
private String sql;
private List parameters;
//设置连接
public void setCon(Connection con) {
this.con = con;
}
//设置sql语句;
public void setSql(String sql) {
this.sql = sql;
}
//设置sql语句参数;
public void setParameters(List aparametersrameters) {
this.parameters = aparametersrameters;
}
/**
* 执行查询;
* @return
*/
public Result executeQuery(){
Result result = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
//如果sql语句有参数;
if(parameters != null && parameters.size() > 0){
pstmt = con.prepareStatement(sql);
setParameters(pstmt, parameters);
rs = pstmt.executeQuery();
}
//如果没有参数;
else{
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
//将ResultSet转换成Result;
result = ResultSupport.toResult(rs);
return result;
}
/**
* 执行更新;
* @return 返回受影响行数;
*/
public int executeUpdate(){
int noOfRows = 0;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
//如果参数不为空;
if(parameters != null && parameters.size() > 0){
pstmt = con.prepareStatement(sql);
setParameters(pstmt, parameters);
noOfRows = pstmt.executeUpdate();
}
else{
pstmt = con.prepareStatement(sql);
noOfRows = pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return noOfRows;
}
/**
* 方法,为sql语句设置参数;
* @param pstmt 指令
* @param parameters 参数集合;
* @throws SQLException
*/
private void setParameters(PreparedStatement pstmt,List parameters) throws SQLException{
for (int i = 0; i < parameters.size(); i++) {
Object v = parameters.get(i);
pstmt.setObject(i+1, v);
}
}
}