mysql java工具类_JAVA之JDBC工具类-Mysql(简单版)

分享一下自己最近写的一个JDBC工具类吧,不对之处还请各位不吝赐教

在项目的目录下创建config文件夹,在里面新建文件保存jdbc的配置信息,这样做便于修改

ca4d210c57fa72c3fd26cfe9a1e6688c.png

jdbc.properties内容:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/xianfeng?useUnicode=true&characterEncoding=utf8

user=root

password=root

xianfeng就是对应数据库的名字

用户和密码要改成自己的哦

5e4b637e08fc136bf31d01b923c18f8b.png

在src下新建一个util包,分别对应三个类Config、jdbcTest、jdbcUtil。

c477018a4f203c2e334c02b1de0dfc93.png

Config用于读取配置文件的方法

package util;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

public class Config {

private Properties prop = new Properties();

public Config(String file)

{

try

{

InputStream in = new FileInputStream(file);

prop.load(in);

}

catch (IOException e)

{

e.printStackTrace();

}

}

public String getValue(String key)

{

return prop.getProperty(key);

}

}

jdbcUtil主要用于数据库方法的封装

package util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class JdbcUtils {

private static String driverClass ;

private static String url ;

private static String user ;

private static String password ;

static{

Config config = new Config("config/jdbc.properties");

//读取配置信息

driverClass = config.getValue("driver");

url = config.getValue("url");

user = config.getValue("user");

password = config.getValue("password");

/**

* 驱动注册

*/

try {

Class.forName(driverClass);//注册加载驱动

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

/**

* 获取 Connetion

* @return

* @throws SQLException

*/

public static Connection getConnection() throws SQLException{

Connection conn = DriverManager.getConnection(url, user, password);

return conn;

}

/**

* 关闭ResultSet资源

* @param rs

*/

public static void close(ResultSet rs){

if(null != rs){

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 关闭Statement资源

* @param stmt

*/

public static void close(Statement stmt){

if(null != stmt){

try {

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 关闭close资源

* @param conn

*/

public static void close(Connection conn){

if(null != conn){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 关闭资源

* @param rs

* @param stmt

* @param conn

*/

public static void close(ResultSet rs,Statement stmt,Connection conn){

close(rs);

close(stmt);

close(conn);

}

}

jdbcTest主要对工具类的测试

package util;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class jdbcTest {

public static void main(String[] args) {

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

//获取连接

conn = JdbcUtils.getConnection();

//编写sql

String sql = "select * from t_bingli";

//创建语句执行者

stmt = conn.prepareStatement(sql);

rs = stmt.executeQuery();

while(rs.next()) {

//输出结果中的第一列和第二列

System.out.println(rs.getString(1)+"+++"+rs.getString(2));

}

} catch (SQLException e) {

e.printStackTrace();

}finally {

JdbcUtils.close(rs, stmt, conn);

}

}

}

结果:

7f3acbfb6a57c9a6875cf8ab46dbb040.png

对应到数据库的信息:

d8c8d47366cacb088e9154db835cbcdd.png

现在大概应该掌握了如何使用这个简单的jdbc工具类了吧,赶紧去试试吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值