javaweb JDBC 数据库连接mysql 配置代码 (直接可用)

1、加入Mysql 的JDBC架包
百度网盘下载链接 : mysql JDBC架包


2、数据库配置文件 db.properties

username=root
userpwd=123456
dbtype=mysql
dburl=jdbc:mysql://localhost:3306/gj
dbdriver=com.mysql.jdbc.Driver

3、数据库配置文件读取ReadProperties.java

package conndb;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//读取数据库配置文件
public class ReadProperties {
private String username;
private String userpwd;
private String dbtype;
private String dburl;
private String dbdriver;
private static ReadProperties readproperties = null;
// 单例
private ReadProperties() {
readpropertiesfile();
}
// 锁防止同时进入,只new一次,只读一次配置文件
public static synchronized ReadProperties instance() {
if (readproperties == null) {
readproperties = new ReadProperties();
}
return readproperties;
}
// 读取文件
private void readpropertiesfile() {
Properties properties = new Properties();
try {
// 获取配置文件的二进制流
InputStream inStream = getClass().getResourceAsStream(
"/db.properties");
// 加载
properties.load(inStream);
// 依次读取
username = properties.getProperty("username");
userpwd = properties.getProperty("userpwd");
dbtype = properties.getProperty("dbtype");
dburl = properties.getProperty("dburl");
dbdriver = properties.getProperty("dbdriver");
} catch (IOException e) {
e.printStackTrace();
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getDbtype() {
return dbtype;
}
public void setDbtype(String dbtype) {
this.dbtype = dbtype;
}
public String getDburl() {
return dburl;
}
public void setDburl(String dburl) {
this.dburl = dburl;
}
public String getDbdriver() {
return dbdriver;
}
public void setDbdriver(String dbdriver) {
this.dbdriver = dbdriver;
}
public static ReadProperties getReadproperties() {
return readproperties;
}
public static void setReadproperties(ReadProperties readproperties) {
ReadProperties.readproperties = readproperties;
}
}

4、数据库连接工厂类 ConnectionFactory.java

package conndb;
import java.sql.Connection;
import java.sql.DriverManager;
//数据库连接工厂,封装数据库从读取配置文件到连接方法
public class ConnectionFactory {
private static final ConnectionFactory factory = new ConnectionFactory();
Connection conn;
private ConnectionFactory() {
}
public static ConnectionFactory getInstance() {
return factory;
}
// 数据库连接方法
public Connection makeConnection() {
ReadProperties rp = ReadProperties.instance();
try {
Class.forName(rp.getDbdriver());
conn = DriverManager.getConnection(rp.getDburl(), rp.getUsername(),
rp.getUserpwd());
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}

5、数据基础操作封装 BaseDao.java
一共封装成两个方法
1. num(sql,param) :只进行增删改操作 ,用的是 executeUpdate(),返回值为影响的记录条数int
2. rs(sql,param) :只进行查询操作,用的是executeQuery(),返回值为结果集ResultSet

package conndb;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//basedao封装了对数据库操作可以返回的两种值,一个是有具体数据集合的,一种是直接判断有没有成功操作的,这样可以节约很多冗余代码
public class BaseDao {
Connection conn = null;
PreparedStatement ps;
// 返回值为结果集的数据库操作方法rs
public ResultSet rs(String sql, String[] param) {
conn = ConnectionFactory.getInstance().makeConnection();
try {
if (conn == null) {
throw new SQLException("conn is null");
}
ps = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
ps.setString(i + 1, param[i]);
}
}
return ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();// 数据库事务回滚
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
// 不存在返回具体值的数据库操作,用int做返回值判断影响数据的的行数,即做增改删操作影响的行数
// 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句
public int num(String sql, String[] param) {
try {
conn = ConnectionFactory.getInstance().makeConnection();
if (conn == null) {
throw new SQLException("conn is null");
}
ps = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
ps.setString(i + 1, param[i]);
}
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();// 数据库事务回滚
} catch (Exception e2) {
e2.printStackTrace();
}
}
return -1;
}
// 关闭数据库连接
public void closeall() {
try {
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

(本代码来自一些教程学习后自己进行的总结整理,直接可用)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值