Java连接MySQL数据库
  TEZNKK3IfmPf 2023年11月14日 19 0
1 1、创建配置文件jdbc.properties
2
3 jdbc.driver=com.mysql.jdbc.Driver
4 jdbc.url=jdbc:mysql://localhost:3306/shop
5 jdbc.username=root
6
7 jdbc.password=123456
8
9 2、读取配置文件类
10
11 package com.hx.shopping.util;
12
13 import java.io.IOException;
14
15 import java.util.Properties;
16
17 public class PropUtil {
18 private static Properties prop = null;
19 public static Properties getProp(String fileName){
20 if(prop==null){
21 prop = new Properties();
22 try {
23 //PropUtil.class.getClassLoader()得到src根路径
24 prop.load(PropUtil.class.getClassLoader().getResourceAsStream(fileName));
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28 }
29 return prop;
30 }
31 }
32
33 3、连接MySQL类
34
35 package com.hx.shopping.util;
36
37 import java.sql.CallableStatement;
38 import java.sql.Connection;
39 import java.sql.DriverManager;
40 import java.sql.ResultSet;
41 import java.sql.SQLException;
42 import java.util.Properties;
43
44 public class DBUtil {
45 static Properties prop = PropUtil.getProp("jdbc.properties");//只写等号后半部分无法识别
46 /*
47 * 加载驱动
48 */
49 public static final String driver = prop.getProperty("jdbc.driver");
50 /*
51 * 连接数据库的URL,端口号是数据库的端口号:3306,而不是8080
52 */
53 public static final String url = prop.getProperty("jdbc.url");
54 /*
55 * 用户名
56 */
57 public static final String username = prop.getProperty("jdbc.username");
58 /*
59 * 密码
60 */
61 public static final String password = prop.getProperty("jdbc.password");
62 /*
63 * 使用静态代码块加载驱动
64 */
65 static{
66 try {
67 Class.forName(driver);
68 System.out.println("加载驱动成功!!");
69 } catch (ClassNotFoundException e) {
70 e.printStackTrace();
71 throw new RuntimeException("加载驱动失败!!!");
72 }
73 }
74 /*
75 * 连接数据的方法
76 */
77 public static Connection getConn(){
78 Connection conn = null;
79 try {
80 conn = DriverManager.getConnection(url, username, password);
81 System.out.println("数据库连接成功!!");
82 } catch (SQLException e) {
83 e.printStackTrace();
84 throw new RuntimeException("数据库连接失败!!");
85 }
86 return conn;
87 }
88 /*
89 * 关闭数据库连接
90 */
91 public static void close(Connection conn,CallableStatement cs,ResultSet rs){
92 try {
93 if(rs != null){
94 rs.close();
95 }
96 if(cs != null){
97 cs.close();
98 }
99 if(conn != null){
100 conn.close();
101 }
102 } catch (SQLException e) {
103 e.printStackTrace();
104 }
105 }
106 /*
107 * 增删改的实现
108 */
109 public static int changeData(Object[] params,String sql){
110 Connection conn = null;
111 CallableStatement cs = null;
112 conn = getConn();
113 int num = -1;
114 try {
115 cs = conn.prepareCall(sql);
116 for(int i=0;i<params.length;i++){
117 cs.setObject((i+1), params[i]);
118 }
119 num = cs.executeUpdate();
120 } catch (SQLException e) {
121 e.printStackTrace();
122 } finally {
123 try {
124 if(cs != null){
125 cs.close();
126 }
127 if(conn != null){
128 conn.close();
129 }
130 } catch (SQLException e) {
131 e.printStackTrace();
132 }
133 }
134 return num;
135 }
136

#label#标签的用法和作用

提高用户体验

放在表格单元格前面,准确定位表格文本框或者选择框。

例子:

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>测试页面</title>
6 </head>
7 <body>
8 <input id="ye" type="checkbox" /> 无label文本
9 <label for="ye1"><input id="ye1" type="checkbox" />
10 有label文本</label>
11
12 </body>
13 </html>

效果截图

Java连接MySQL数据库

第一个鼠标放在文字上单击不会触发选择框

第二个鼠标在文字上单击可以触发选择框

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   21天前   48   0   0 java
TEZNKK3IfmPf