丁一鸣的CSDN

跬步=>千里

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();
}
}
}

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

阅读更多
版权声明:如你想,请分享 https://blog.csdn.net/iMDingDing/article/details/46679291
个人分类: 【javaweb】
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭