JDBC数据库操作工具类——JdbcUtils

如果经常使用 JDBC(Java DataBase Connectivity) 连接到数据库,那就把这个功能做成一个工具类,可以在不同的地方重复使用

例如:创建一张学生表 student

package class01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo01 {
public static void main(String[] args){
Connection conn = null;
Statement statement = null;
String url = "jdbc:mysql://localhost:3306/db_test";
String user = "root";
String pwd = "lemon";
try {
conn = DriverManager.getConnection(url,user,pwd);//创建数据库连接
statement = conn.createStatement();//创建一条 SQL 语句对象
statement.executeUpdate("create table student (id int primary key auto_increment,name varchar (20) not null,birthday date)");//用户发送 DML 语句,增删改的操作
System.out.println("创建成功!");
} catch (SQLException e) {
e.printStackTrace();
}finally {//释放资源
if (statement != null){
try {
statement.close();//后开的先关
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

在上面的代码中,有很多重复的代码,可以把这些公共的代码抽取出来,创建一个 JdbcUtil 数据库工具类

一、创建 JdbcUtil 工具类

1、包含3个方法

可以把几个字符串定义成常量:用户名,密码,URL,驱动类
得到数据库的连接getConnection()
关闭所有打开的资源:close(Connection conn, Statement stmt) 或者close(Connectionconn, Statement stmt, ResultSet rs)
2、代码

package jdbcutil;

import java.sql.*;

public class JdbcUtils {
//把用户名、密码、URL、驱动类 这几个字符串定义为常量
private static final String USER = "root";
private static final String PWD = "lemon";
private static final String URL = "jdbc:mysql://localhost:3306/db_test";
private static final String DRIVER = "com.mysql.jdbc.Driver";

//注册驱动
static {//静态代码块,只执行一次
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

//得到数据库的连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL,USER,PWD);
}

//关闭所有打开的资源
public static void close(Connection conn, Statement stm){
if (stm != null){
try {
stm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

//关闭所有打开的资源
public static void close(Connection conn, Statement stm, ResultSet rs){
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stm != null){
try {
stm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

利用创建好的工具类 输出表

package jdbcutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo01 {
public static void main(String[] args){
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery("select * from student");
while (rs.next()){
System.out.println("编号:" + rs.getInt("id") +
"姓名:" + rs.getString("name") +
"性别:" + rs.getString("sex") +
"生日:" + rs.getDate("birthday") +
"分数:" + rs.getInt("num"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(conn,stm,rs);
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
利用工具类向表中插入一条数据

package jdbcutil;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo01 {
public static void main(String[] args){
Connection conn = null;
Statement stm = null;
try {
conn = JdbcUtils.getConnection();
stm = conn.createStatement();
stm.executeUpdate("insert into student(name,sex,birthday,num) values ('猛哥','男','1991-10-6',100)");
} catch (SQLException e) {
e.printStackTrace(http://www.my516.com);
}finally {
JdbcUtils.close(conn,stm);
}
}
}

---------------------

posted @ 2019-07-24 01:28  水至清明  阅读(1640)  评论(0编辑  收藏  举报