时间:2021-07-01 10:21:17 帮助过:18人阅读
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.sql.Statement;
6 import java.util.ResourceBundle;
7
8 public class JDBCUtil {
9 //ctrl + shift + x 转成大写
10 //ctrl + shift + y 转成小写
11 static final String DRIVERNAME;
12 static final String URL;
13 static final String USERNAME;
14 static final String PASSWORD;
15
16 static{
17
18 /*通过ResourceBundle 专门用来加载properties文件
19 ResourceBundle bundle=ResourceBundle.getBundle("文件名称");
20
21 通过 bundle.getString(键的名字)
22 String value=bundle.getString("url");
23 */
24
25 ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
26 DRIVERNAME=bundle.getString("drivername");
27 URL=bundle.getString("url");
28 USERNAME=bundle.getString("user");
29 PASSWORD=bundle.getString("password");
30 }
31
32 static{
33 try {
34 Class.forName(DRIVERNAME);
35 } catch (ClassNotFoundException e) {
36 e.printStackTrace();
37 }
38 }
39
40 //获取链接
41 public static Connection getConnection() throws SQLException{
42 return DriverManager.getConnection(URL,USERNAME,PASSWORD);
43 }
44
45 //释放资源
46 public static void closeResource(Connection conn,Statement st,ResultSet rs){
47 if (rs!=null) {
48 try {
49 rs.close();
50 } catch (SQLException e) {
51 e.printStackTrace();
52 }
53 }
54
55 if (st!=null) {
56 try {
57 st.close();
58 } catch (SQLException e) {
59 e.printStackTrace();
60 }
61 }
62
63 if (conn!=null) {
64 try {
65 conn.close();
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }
69 }
70
71 }
72 }
(3)CRUDDemo, 使用PreparedStatement方式
1 public class PPCRUDDemo {
2 public static void main(String[] args) {
3 //插入
4 //insert("赵四","123","zhaosi@163.com");
5 //更新
6 //updateByName("赵四","尼古拉斯.赵四");
7 //获取
8 //getByName("尼古拉斯.赵四");
9 //删除
10 deleteByName("尼古拉斯.赵四");
11 }
12
13 private static void deleteByName(String string) {
14 //模版
15 Connection conn=null;
16 PreparedStatement st=null;
17 ResultSet rs=null;
18
19 try {
20 //获取链接
21 conn=JDBCUtil.getConnection();
22 //编写sql
23 String sql="delete from user where username =?";
24 //获取预编译执行者
25 st=conn.prepareStatement(sql);
26 //设置参数
27 st.setString(1, string);
28 //执行sql
29 int i = st.executeUpdate();
30 //处理结果
31 if (i>0) {
32 System.out.println("成功");
33 }else{
34 System.out.println("失败");
35 }
36 } catch (Exception e) {
37 e.printStackTrace();
38 }finally{
39 //释放资源
40 JDBCUtil.closeResource(conn, st, rs);
41 }
42
43 }
44
45 private static void getByName(String string) {
46
47 Connection conn=null;
48 PreparedStatement st=null;
49 ResultSet rs=null;
50
51 try {
52 conn=JDBCUtil.getConnection();
53 String sql="select * from user where username=? limit 1";
54 st=conn.prepareStatement(sql);
55
56 st.setString(1, string);
57 rs=st.executeQuery();
58 if (rs.next()) {
59 System.out.println(rs.getInt(1)+":"+rs.getString(2)+":"+rs.getObject(3)+":"+rs.getObject(4));
60 }
61 } catch (Exception e) {
62 // TODO: handle exception
63 e.printStackTrace();
64 }finally{
65
66 JDBCUtil.closeResource(conn, st, rs);
67 }
68
69 }
70
71 private static void updateByName(String oldName, String newName) {
72 Connection conn=null;
73 PreparedStatement st=null;
74 ResultSet rs=null;
75
76 try {
77 conn=JDBCUtil.getConnection();
78 String sql="update user set username = ? where username = ?";
79 st=conn.prepareStatement(sql);
80 st.setString(1, newName);
81 st.setString(2, oldName);
82
83 int i=st.executeUpdate();
84 //处理结果
85 if (i>0) {
86 System.out.println("成功");
87 }else{
88 System.out.println("失败");
89 }
90
91 } catch (Exception e) {
92 e.printStackTrace();
93 }finally{
94 JDBCUtil.closeResource(conn, st, rs);
95 }
96
97
98 }
99
100 private static void insert(String username, String password, String email) {
101 Connection conn=null;
102 PreparedStatement st=null;
103 ResultSet rs=null;
104
105 try {
106 //获取链接
107 conn=JDBCUtil.getConnection();
108 //编写sql
109 String sql="insert into user values(null,?,?,?)";
110 //获取预编译语句执行者
111 st=conn.prepareStatement(sql);
112 //设置参数
113 st.setString(1, username);
114 st.setString(2, password);
115 st.setString(3, email);
116 //执行sql
117 int i=st.executeUpdate();
118 //处理结果
119 if (i>0) {
120 System.out.println("成功");
121 }else{
122 System.out.println("失败");
123 }
124 } catch (Exception e) {
125 e.printStackTrace();
126 }finally{
127 JDBCUtil.closeResource(conn, st, rs);
128 }
129 }
130
131 }
这里没有多么高深的东西, 熟能生巧, 暂时总结的就这么多.
[数据库操作]Java中的JDBC的使用方法.
标签:语句 数据库操作 一个 cep host 需要 api getc user