我是靠谱客的博主 超级小熊猫,这篇文章主要介绍Java Swing JTable实现增删改查 DBUtils 工具类补充DBUtils 工具类,现在分享给大家,希望可以做个参考。

DBUtils 工具类

看见很多同学,浏览了(Java Swing JTable 实现增删改查),这篇文章,想要运行一下看下效果,但是由于缺少DUtils 工具类,无法连接数据库进行测试,因此特地做出补充。

以下为DBUtils工具类的源码补充

复制代码
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.sql.*; public class DBUtils { //URL的填写请自行百度,根据数据库版本搜索 private final static String URL = "your url"; //填写你的数据库连接用户名 private final static String ROOT = "your name"; //填写你的数据库连接密码 private final static String PASSWORD = "your password"; private static Connection conn; private static PreparedStatement ps; private static ResultSet rs; //加载驱动,获取连接 public static Connection getConn() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection(URL, ROOT, PASSWORD); } /** * @param sql:执行的sql * @param params:设置参数 * @return */ //执行增删改语句,这里使用Object[]数组是为了满足不同参数传递的需要 public static boolean executeUpdate(String sql, Object[] params){ boolean flag = false; try { conn = getConn(); ps = conn.prepareStatement(sql);//预编译SQL if (params != null) { for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } } int count = ps.executeUpdate();//执行SQL if (count > 0) flag = true; else flag = false; return flag; } catch (SQLException s) { s.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return flag; } //执行查询语句 public static ResultSet executeQuery(String sql, Object[] params) { try { conn = getConn(); ps = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } } rs = ps.executeQuery(); return rs; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException s) { s.printStackTrace(); } return rs; } //关闭流,由于参数个数不同,这里使用了重载 public static void close(Connection conn, PreparedStatement ps, ResultSet rs) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn, PreparedStatement ps) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } //这里对代码进行测试,测试数据库是否连接成功,没有输出就是最好的消息 public static void main(String[] args) { try { DBUtils.getConn(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }

最后

以上就是超级小熊猫最近收集整理的关于Java Swing JTable实现增删改查 DBUtils 工具类补充DBUtils 工具类的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部