DbUtil(数据的增删改查以及分页)

DbUtil 是一个用于数据库增删改查和分页查询的实用工具,它简化了常见的数据库交互操作,提高了开发效率。通过 DbUtil,开发者可以方便地执行 CRUD 操作,并实现高效的数据分页展示。
摘要由CSDN通过智能技术生成
package com.zit.dao;

import java.sql.*;
import java.util.*;
/**
 * Created by webrx on 2017-08-16.
 */
public class DbUtil {
    private String driver = "com.mysql.jdbc.Driver"; // 数据库驱动类
    private String url = "jdbc:mysql://localhost:3306/oadb?useUnicode=true&characterEncoding=utf8&useSSL=true";// url
    private String uid = "root"; // 账号
    private String pwd = "123456";// 密码
    private int port = 3306;
    private Connection conn = null;
    private int currpage = 1;
    private int pagesize = 5;
    private int recordcount = 0;
    private int pagecount = 0;
    private String dbname = "oadb";
    private String host = "localhost";


    /**
     * host localhost
     * user root
     * pwd  123456
     * port 3306
     */
    public DbUtil() {
        try {
            Properties pro = new Properties();
            pro.load(DbUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            this.driver = pro.getProperty("db.driver");
            Class.forName(this.driver);
            this.url = pro.getProperty("db.url");
            this.uid = pro.getProperty("db.user");
            this.pwd = pro.getProperty("db.password");
            this.conn = DriverManager.getConnection(this.url, this.uid, this.pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DbUtil(String host,String user,String password,String dbname) {
        this.host = host;
        this.uid = user;
        this.pwd = password;
        this.url = String.format("jdbc:mysql://%s:3306/%s?useUnicode=true&characterEncoding=utf8&useSSL=true",this.host,this.dbname);
        try {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(url, uid, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void connect(String host, String user, String password, int port, String dbname) {
        this.host = host;
        this.url = url;
        this.uid = user;
        this.pwd = password;
        this.dbname = dbname;
        this.port = port;
        this.url = String.format("jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf8&useSSL=true",this.host,this.port,this.dbname);
        try {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(url, uid, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Stri
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值