Java JDBC连接mysql数据库
时间:2021-07-01 10:21:17
帮助过:9人阅读
import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import com.mysql.jdbc.PreparedStatement;
6
7 public class DBTool {
8
9 public static final String DRIVER ="com.mysql.jdbc.Driver"
;
10 public static final String URL = "jdbc:mysql://localhost:3306/数据库名"
;
11 public static final String USERNAME = "用户名"
;
12 public static final String PSW = "密码"
;
13
14 public static Connection getConn(){
15 Connection conn =
null;
16 try {
17 Class.forName(DRIVER);
18 conn =
DriverManager.getConnection(URL,USERNAME,PSW);
19 }
catch (ClassNotFoundException e) {
20 e.printStackTrace();
21 }
catch (SQLException e) {
22 e.printStackTrace();
23 }
24 return conn;
25 }
26 //关闭资源
27 public static void closeAll(ResultSet rs, PreparedStatement ps,Connection conn){
28 if(rs!=
null){
29 try {
30 rs.close();
31 }
catch (SQLException e) {
32 e.printStackTrace();
33 }
34 }
35 if(ps!=
null){
36 try {
37 rs.close();
38 }
catch (SQLException e) {
39 e.printStackTrace();
40 }
41 }
42 if(conn!=
null){
43 try {
44 conn.close();
45 }
catch (SQLException e) {
46 e.printStackTrace();
47 }
48 }
49 }
50
51 }
Java JDBC连接mysql数据库
标签:col l数据库 ati turn 密码 import return otf manager