1 package util; 2 3 import java.sql.DriverManager; 4 import java.sql.SQLException; 5 6 import com.mysql.jdbc.*; 7 8 9 10 public class DBConfgier { 11 12 private static final String driver = "com.mysql.jdbc.Driver"; 13 //定义连接URL、数据用户及密码 14 private static final String dbURL = "jdbc:mysql://localhost:3306/School_info?characterEncoding=utf8&useSSL=true&serverTimezone=UTC"; 15 private static final String dbUser = "root"; 16 private static final String dbPassword = "root"; 17 18 private static Connection conn = null; 19 20 21 //载入JDBC驱动程序 22 static 23 { 24 try 25 { 26 Class.forName("com.mysql.jdbc.Driver"); 27 } catch(Exception ex) { 28 ex.printStackTrace(); 29 } 30 } 31 32 33 public static Connection getConnnection() throws Exception { 34 if(conn == null) { 35 conn = (Connection) DriverManager.getConnection(dbURL, dbUser, dbPassword); 36 return conn; 37 } 38 return conn; 39 } 40 41 public static void main(String[] args) throws Exception { 42 Connection conn = DBConfgier.getConnnection(); 43 if(conn != null) { 44 System.out.println("连接成功!"); 45 } else { 46 System.out.println("连接失败!"); 47 } 48 } 49 50 51 52 }