Druid和DbUtils的封装及使用

//导jar包 [druid、dbutils、mysql]
MyDbProperties.properties: 
	driverClassName=com.mysql.cj.jdbc.Driver  
	url=jdbc:mysql://localhost:3306/test?useSSL=false&useServerPrepStmts=true  
	username=root  
	password=1234  
	initialSize=5  
	maxActive=10  
	maxWait=3000
User : 
	public class User implements Serializable {  
	    private static final long serialVersionUID = -7020619477594468968L;  
	    private int id;  
	    private String username;  
	    private String password;  
	  
	    public User() {  
	    }  
	  
	    public User(String username, String password) {  
	        this.username = username;  
	        this.password = password;  
	    }  
	  
	    public User(int id, String username, String password) {  
	        this.id = id;  
	        this.username = username;  
	        this.password = password;  
	    }  
	  
	    public int getId() {  
	        return id;  
	    }  
	  
	    public void setId(int id) {  
	        this.id = id;  
	    }  
	  
	    public String getUsername() {  
	        return username;  
	    }  
	  
	    public void setUsername(String username) {  
	        this.username = username;  
	    }  
	  
	    public String getPassword() {  
	        return password;  
	    }  
	  
	    public void setPassword(String password) {  
	        this.password = password;  
	    }  
	  
	    @Override  
	    public String toString() {  
	        return "User{" +  
	                "id=" + id +  
	                ", username='" + username + '\'' +  
	                ", password='" + password + '\'' +  
	                '}';  
	    }  
	}
MyDbUtils.java : 
	public class MyDbUtils {  
	    protected static DruidDataSource druidDataSource;  
	    protected static Connection connection;  
	    //加载配置  
	    static {  
	        Properties properties = new Properties();  
	        InputStream is = MyDbUtils.class.getResourceAsStream("MyDbProperties.properties");  
	        try {  
	            properties.load(is);  
	            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }  
	    //获取资源  
	    protected static DruidDataSource getDruidDataSource(){  
	        return druidDataSource;  
	    }  
	    protected Connection getConnection(){  
	        try {  
	            connection=druidDataSource.getConnection();  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	        return connection;  
	    }  
	    protected static QueryRunner queryRunner(){  
	        return new QueryRunner(MyDbUtils.getDruidDataSource());  
	    }  
	    //增删改  
	    protected static int updateData(String sql,Object... params){  
	        int numberOfRowsAffected=0;  
	        try {  
	            numberOfRowsAffected = queryRunner().update(sql, params);  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	        return numberOfRowsAffected;  
	    }  
	}
DbUtils.java : 
	public class DbUtils extends MyDbUtils{  
	    private static final String TABLENAME="qh_student";  
	    //添加  
	    public static int insert(User user) throws SQLException {  
	        String sql = "insert into "+TABLENAME+"(username,password) " +  
	                "values (?,?)";  
	        Object[] params = {user.getUsername(),user.getPassword()};  
	        int influenceNumber = updateData(sql, params);  
	        return influenceNumber;  
	    }  
	    //删除  
	    public static int delete(int id) throws SQLException {  
	        String sql = "delete from "+TABLENAME+" where id=?";  
	        int influenceNumber = updateData(sql, id);  
	        return influenceNumber;  
	    }  
	    //更新  
	    public static int update(User user) throws SQLException {  
	        String sql = "update "+TABLENAME+" set username=?,password=? where id=?";  
	        Object[] params = {user.getUsername(),user.getPassword(),user.getId()};  
	        int influenceNumber = updateData(sql, params);  
	        return influenceNumber;  
	    }  
	    //计算总数  
	    public static int count() throws SQLException {  
	        return findAllResult().size();  
	    }  
	    //获取所有  
	    public static List<User> findAllResult() throws SQLException {  
	        String sql = "select * from "+TABLENAME+"";  
	        QueryRunner queryRunner = new QueryRunner(getDruidDataSource());  
	        BeanListHandler<User> beanListHandler = new BeanListHandler<>(User.class);  
	        List<User> list = queryRunner.query(sql, beanListHandler);  
	        return list;  
	    }  
	    //分页  
	    public static List<User> findLimitResult(int len,int begin) throws SQLException {  
	        String sql = "select * from "+TABLENAME+" limit "+len+" offset "+begin+"";  
	        QueryRunner queryRunner = new QueryRunner(getDruidDataSource());  
	        BeanListHandler<User> beanListHandler = new BeanListHandler<>(User.class);  
	        List<User> list = queryRunner.query(sql, beanListHandler);  
	        return list;  
	    }  
	    //根据id获取  
	    public static User accordingToId(int id) throws SQLException {  
	        String sql = "select * from "+TABLENAME+" where id=?";  
	        QueryRunner queryRunner = new QueryRunner(getDruidDataSource());  
	        BeanHandler<User> beanHandler = new BeanHandler<>(User.class);  
	        User user = queryRunner.query(sql, beanHandler, id);  
	        return user;  
	    }  
	    //根据username获取  
	    public static User accordingToUserName(String username) throws SQLException {  
	        String sql = "select * from "+TABLENAME+" where username=?";  
	        QueryRunner queryRunner = new QueryRunner(getDruidDataSource());  
	        BeanHandler<User> beanHandler = new BeanHandler<>(User.class);  
	        User user = queryRunner.query(sql, beanHandler, username);  
	        return user;  
	    }  
	}
Demo.java :
	public class Main {  
	    public static void main(String[] args) throws SQLException {  
	        System.out.println(DbUtils.insert(new User("阿丑","ac")));  
	        System.out.println("----------");  
	        System.out.println(DbUtils.delete(16));  
	        System.out.println("----------");  
	        System.out.println(DbUtils.update(new User(17,"刘亦菲","lyf")));  
	        System.out.println("----------");  
	        System.out.println(DbUtils.count());  
	        System.out.println("----------");  
	        System.out.println(DbUtils.findAllResult());  
	        System.out.println("----------");  
	        System.out.println(DbUtils.findLimitResult(4, 1));  
	        System.out.println("----------");  
	        System.out.println(DbUtils.accordingToId(16));  
	        System.out.println("----------");  
	        System.out.println(DbUtils.accordingToUserName("东方不败"));  
	    }  
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钱要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值