使用回调函数来实现jdbc操作数据库数据

package com.tools;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
  
/**
 * jdbc4 不用加载驱动  及不用Class.forName("");  看 驱动jar下面META-INF下有没有services/java.sql.Driver文件  纯在这不用加载
 * 
 * @author lilin
 *
 */
public class DButils { 
/**
* 获取连接
* @return
*/
public static Connection getcon() { 
try {

return DriverManager.getConnection("jdbc:mysql://localhost:3306/testdbs","root","123456");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

return null;

}


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 T getTinfo(String sql,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] );


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 getuserinfo(String sql) throws SQLException{ 

BaseDao  b= new BaseDao ();
return b.getTinfo(sql, new IHandler>() {

@Override
public List getTinfo(ResultSet rs) throws SQLException {
List list= new ArrayList();
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 list= udao.getuserinfo("select * from lilin_userinfo"); 
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); 

}
}




你可能感兴趣的:(java,回调函数,jdbc)