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
DbUtil(数据的增删改查以及分页)
最新推荐文章于 2020-10-24 23:06:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)