}
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 回调基类
* @author lilin
*
* @param
*/
public interface IHandler
public T getTinfo( ResultSet rs ) throws SQLException;
public int updateTinfo( int rs ) throws SQLException;
}
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.tools.DButils;
/**
* 数据库操作基类
* @author lilin
*
*/
public class BaseDao {
/**
* 查询。。。。。
* @param sql
* @param ihander
* @param params
* @return
* @throws SQLException
*/
public
Connection con = DButils.getcon();
if( null!=con){
PreparedStatement ps = con.prepareStatement(sql);
for(int i= 0,num =params.length ; i
ps.setObject( i+1 , params[i] );
}
ResultSet rs=ps.executeQuery();
T t= ihander.getTinfo(rs);
if(null != rs){rs.close();}
if(null != ps){ps.close();}
if(null != con){con.close();}
return t;
}else{
return null;
}
}
/**
* 修改
* @param sql
* @param ihander
* @param params
* @return
* @throws SQLException
*/
public int updateTinfo(String sql,@SuppressWarnings("rawtypes") IHandler ihander,Object...params) throws SQLException{
Connection con = DButils.getcon();
if(null!=con){
PreparedStatement ps = con.prepareStatement(sql);
for(int i= 0,num =params.length ; i
ps.setObject( i+1 , params[i] );
}
int num=ps.executeUpdate();
int t= ihander.updateTinfo(num);
if(null != ps){ps.close();}
if(null != con){con.close();}
return t;
}else{
return 0;
}
}
}
===============
实体
package com.entity;
public class Userinfo {
private Integer userid;
private String username;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
===============
测试和实现类
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Userinfo;
public class UserinfoDao {
/**
* 查询全部信息
* @param sql
* @return
* @throws SQLException
*/
public List
BaseDao b= new BaseDao ();
return b.getTinfo(sql, new IHandler>() {
@Override
public List
List
Userinfo userinfo = null;
while(rs.next()){
userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
list.add(userinfo);
}
return list;
}
@Override
public int updateTinfo(int rs) throws SQLException {
return 0;
}
});
}
/**
* 查询单个 不带参数
* @param sql
* @return
* @throws SQLException
*/
public Userinfo getoneuserinfo(String sql) throws SQLException{
BaseDao b= new BaseDao ();
return b.getTinfo(sql, new IHandler
@Override
public Userinfo getTinfo(ResultSet rs) throws SQLException {
Userinfo userinfo = null;
while(rs.next()){
userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
}
return userinfo;
}
@Override
public int updateTinfo(int rs) throws SQLException {
return 0;
}
} );
}
/**
* 查询单个 带参数
* @param sql
* @param params
* @return
* @throws SQLException
*/
public Userinfo getoneuserinfobyid(String sql, Object...params) throws SQLException{
BaseDao b= new BaseDao ();
return b.getTinfo(sql, new IHandler
@Override
public Userinfo getTinfo(ResultSet rs) throws SQLException {
Userinfo userinfo = null;
while(rs.next()){
userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
}
return userinfo;
}
@Override
public int updateTinfo(int rs) throws SQLException {
return 0;
}
},params );
}
/**
* 修改 删除 添加
* @param sql
* @param params
* @return
* @throws SQLException
*/
public int updateuserinfo( String sql, Object...params ) throws SQLException {
BaseDao b= new BaseDao ();
return b.updateTinfo(sql, new IHandler
@Override
public Userinfo getTinfo(ResultSet rs) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int updateTinfo(int rs) throws SQLException {
return rs;
}
}, params);
}
public static void main(String[] args) throws SQLException {
System.out.println("测试。。。");
UserinfoDao udao =new UserinfoDao();
List
for( int num = list.size() -1,i = num ;i >= 0;i-- ){
Userinfo u = list.get(i);
System.out.print(u.getUserid()+"=======");
System.out.print( u.getUsername() );
System.out.println();
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Userinfo u=udao.getoneuserinfo("select * from lilin_userinfo where userid= 1");
System.out.print(u.getUserid()+"=======");
System.out.print( u.getUsername() );
System.out.println();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Userinfo us=udao.getoneuserinfobyid("select * from lilin_userinfo where userid= ? ",8);
System.out.print(us.getUserid()+"=======");
System.out.print( us.getUsername() );
System.out.println();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
int a= udao.updateuserinfo("insert into lilin_userinfo(username,pid) values(?,?)", "张三x",2);
System.out.println("受影响的行数:"+a);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
int b = udao.updateuserinfo("update lilin_userinfo set username=? where userid = ?", "张三1",9);
System.out.println("受影响的行数:"+b);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
int c = udao.updateuserinfo("delete from lilin_userinfo where userid = ?",10);
System.out.println("受影响的行数:"+c);
}
}