forked from ztgreenleaves/JavaWeatherApplication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBUtil.java
More file actions
41 lines (35 loc) · 918 Bytes
/
Copy pathDBUtil.java
File metadata and controls
41 lines (35 loc) · 918 Bytes
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
package Utils;
import java.sql.*;
public class DBUtil {
public static String classname = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/city"
+"?useUnicode=true&characterEncoding=utf8&";
public static String username = "root";
public static String password = "";
static {
try {
Class.forName( classname );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
Connection conn =
DriverManager.getConnection(url,username,password);
return conn;
}
public static void close( Connection conn ){
if( conn!=null ){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
Connection conn = DBUtil.getConnection();
System.out.println( conn );
DBUtil.close(conn);
}
}