Three methods of connecting MySQL with Java

From , 3 Years ago, written in Java, viewed 227 times.
URL https://pastebin.vip/view/7e1d842d
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.util.Properties;  
  4.  
  5. //java 连接MySql的三种方法
  6. public class ConnectionUtil {
  7.    
  8.         //第一种方法  
  9.         public Connection getConnection(){  
  10.                 Connection conn = null;  
  11.                 try {  
  12.                         //Class.forName加载驱动  
  13.                         Class.forName("com.mysql.jdbc.Driver");  
  14.                         //DriverManager获得连接  
  15.                         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");  
  16.                         return conn;  
  17.                 } catch (Exception e) {  
  18.                         e.printStackTrace();  
  19.                 }  
  20.                 return null;  
  21.         }  
  22.  
  23.  
  24.         //第二种方法  
  25.         public Connection getConnection(String driver,String url,String user,String password){  
  26.                 Connection conn = null;  
  27.                 try {  
  28.                         //Class.forName加载驱动  
  29.                         Class.forName(driver);  
  30.                         //DriverManager获得连接  
  31.                         conn = DriverManager.getConnection(url,user,password);  
  32.                         return conn;  
  33.                 } catch (Exception e) {  
  34.                         e.printStackTrace();  
  35.                 }  
  36.                 return null;  
  37.         }  
  38.  
  39.  
  40.         //第三种方法  
  41.         public Connection openConnection(){  
  42.                 String driver = "";  
  43.                 String url = "";  
  44.                 String user = "";  
  45.                 String password = "";  
  46.                 Properties prop = new Properties();  
  47.                 Connection conn = null;  
  48.                 try {  
  49.                         //加载属性文件  
  50.                         prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));  
  51.                         driver = prop.getProperty("driver");  
  52.                         url = prop.getProperty("url");  
  53.                         user = prop.getProperty("user");  
  54.                         password = prop.getProperty("password");  
  55.                         //Class.forName加载驱动  
  56.                         Class.forName(driver);  
  57.                         //DriverManager获得连接  
  58.                         conn = DriverManager.getConnection(url,user,password);  
  59.                         return conn;  
  60.                 } catch (Exception e) {  
  61.                         e.printStackTrace();  
  62.                 }  
  63.                 return null;  
  64.         }  
  65.  
  66. }
  67.  

Reply to "Three methods of connecting MySQL with Java"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website