DbUtils的简单使用范例

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
DButils是Apache的一个开源数据库访问框架
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import javax.sql.DataSource;
 
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
 
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
 
public class UserDaoImpl implements UserDao  {
     
    public DataSource getDataSource(){
        String url = "jdbc:mysql://127.0.0.1:3306/testdbutils";
        MysqlDataSource ds = new MysqlDataSource();
        ds.setServerName("127.0.0.1");
//      ds.setDatabaseName("testdbutils");
        ds.setURL(url);
        ds.setUser("root");
        ds.setPassword("123456");
        ds.setCharacterEncoding("utf8");
        return ds;
    }
     
    public BeanListHandler<User> getBeanListHandler(){
        return new BeanListHandler<User>(User.class);
    }
     
    public BeanHandler<User> getBeanHandler(){
        return new BeanHandler<User>(User.class);
    }
 
    public void delete(int id) {
        QueryRunner runner = new QueryRunner(getDataSource());
        try {
            int affectedRows = runner.update("delete from user where id = ?",id);
            System.out.println("删除成功,影响的行数:"+affectedRows);
        } catch (SQLException e) {
            System.out.println("删除id为"+id+"的记录失败。错误为:"+e.getMessage());
        }
    }
 
    public void delete(User user) {
        delete(user.getId());
    }
 
    public List<User> getAllUsers() {
        QueryRunner runner = new QueryRunner(getDataSource());
        try {
            return runner.query("select * from user", getBeanListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public User getById(int id) {
        QueryRunner runner = new QueryRunner(getDataSource());
        User user = null;
        try {
            user = runner.query("select * from user where id = ?", getBeanHandler(),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
         
        return user;
    }
 
    public void save(User user) {
        QueryRunner runner = new QueryRunner(getDataSource());
        try {
            runner.update("insert into user values(?,?,?,?,?)",new Object[]{
                    user.getId(),
                    user.getName(),
                    user.getAge(),
                    user.getSex(),
                    user.getBirth()
            });
        } catch (SQLException e) {
            System.out.println("插入失败,失败原因:"+e.getMessage());
        }
    }
 
    public long getCount() {
        QueryRunner runner = new QueryRunner(getDataSource());
        try {
             
             
            Long count = runner.query("select count(*) as count from user", new ResultSetHandler<Long>(){
 
                public Long handle(ResultSet rs) throws SQLException {
                    if(rs.next()){
                        return rs.getLong(1); //或者rs.getLong("count");
                    }
                    return 0L;
                }
                 
            });
             
            //或者用ScalarHandler, 这里的new ScalarHandler() 可以加上“count”参数
//          Long count = (Long)runner.query("select count(*) as count from user",new  ScalarHandler(){
//
//              @Override
//              public Object handle(ResultSet rs) throws SQLException {
//                  return super.handle(rs);
//              }
//              
//          });
 
            return (Long)count;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0L;
        }
    }
     
     
     
}

标签: Mysql 数据库

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: 正则表达式简介与常用表示

下一篇:Java图像查看器