java JDBC鏈接mysql

  1. 下載驅動包:http://dev.mysql.com/downloads/connector/j/,解壓獲得jar文件,例如mysql-connector-java-8.0.11.jar
  2. 在項目下新建文件夾,將jar包放進去,點擊項目右鍵,【構建路徑】——【配置構建路徑】,添加jar文件
  3. 鏈接數據庫
  4. 加粗部分爲新特性
  •  1 public final class DbConn  2 {  3     public static Connection getconn()  4  {  5         Connection conn = null;  6         
     7         String driver = "com.mysql.cj.jdbc.Driver";  8         String user   = "root";  9         String passwd = "root"; 10         String url = "jdbc:mysql://localhost:3306/store?useSSL=false&serverTimezone=UTC";//指定要訪問的數據庫store 11         
    12         //已加載完驅動
    13         try
    14  { 15  Class.forName(driver); 16             conn = DriverManager.getConnection(url,user,passwd); 17         }catch (SQLException e) 18  { 19  e.printStackTrace(); 20  } 21         catch (ClassNotFoundException e) 22  { 23  e.printStackTrace(); 24  } 25         return conn; 26  } 27 
    28 }
     1 public final class DbClose  2 {  3     /**
     4  * 關閉 添加功能 資源  5  * @param pstmt,rs,conn  6      */
     7         public static void addClose(PreparedStatement pstmt, Connection conn)  8  {  9             /*
    10  * 多個 try-catch 出發點:安全 11              */
    12             try
    13  { 14                 if (pstmt != null) 15  { 16  pstmt.close(); 17  } 18             } catch (SQLException e1) 19  { 20  e1.printStackTrace(); 21  } 22             try
    23  { 24                 if (conn != null) 25  { 26  conn.close(); 27  } 28             } catch (SQLException e) 29  { 30  e.printStackTrace(); 31  } 32  } 33         
    34         /**
    35  * 關閉資源 36  * @param pstmt,rs,conn 37          */
    38         public static void queryClose(PreparedStatement pstmt, ResultSet rs, Connection conn) 39  { 40             try
    41  { 42                 if (pstmt != null) 43  { 44  pstmt.close(); 45  } 46             } catch (SQLException e1) 47  { 48  e1.printStackTrace(); 49  } 50             try
    51  { 52                 if (rs != null ) 53  { 54  rs.close(); 55  } 56             } catch (SQLException e1) 57  { 58  e1.printStackTrace(); 59  } 60             try
    61  { 62                 if (conn != null) 63  { 64  conn.close(); 65  } 66             } catch (SQLException e) 67  { 68  e.printStackTrace(); 69  } 70  } 71         
    72 }

    測試代碼。測試是否鏈接成功:java

  •  1 public class Show {  2 
     3     public static void main(String[] args) {  4         //聲明Connection對象
     5  Connection con;  6         //驅動程序名
     7         String driver = "com.mysql.cj.jdbc.Driver";  8         //URL指向要訪問的數據庫名mydata
     9         String url = "jdbc:mysql://localhost:3306/store?useSSL=false&serverTimezone=UTC"; 10         //MySQL配置時的用戶名
    11         String user = "root"; 12         //MySQL配置時的密碼
    13         String password = "root"; 14         //遍歷查詢結果集
    15         try { 16             //加載驅動程序
    17  Class.forName(driver); 18             //1.getConnection()方法,鏈接MySQL數據庫!!
    19             con = DriverManager.getConnection(url,user,password); 20             if(!con.isClosed()) 21                 System.out.println("Succeeded connecting to the Database!"); 22             //2.建立statement類對象,用來執行SQL語句!!
    23             Statement statement = con.createStatement(); 24             //要執行的SQL語句
    25             String sql = "select * from goods where Gid='g231'"; 26             //3.ResultSet類,用來存放獲取的結果集!!
    27             ResultSet rs = statement.executeQuery(sql); 28             System.out.println("-----------------"); 29             System.out.println("執行結果以下所示:"); 30             System.out.println("-----------------"); 31             System.out.println("名稱" + "\t" + "數量"); 32             System.out.println("-----------------"); 33              
    34             String Gname = null; 35             String Gnum = null; 36             while(rs.next()){ 37                 //獲取stuname這列數據
    38                 Gname = rs.getString("Gname"); 39                 //獲取stuid這列數據
    40                 Gnum = rs.getString("Gnum"); 41 
    42                 //輸出結果
    43                 System.out.println(Gname + "\t" + Gnum); 44  } 45  rs.close(); 46  con.close(); 47         } catch(ClassNotFoundException e) { 48             //數據庫驅動類異常處理
    49             System.out.println("Sorry,can`t find the Driver!"); 50  e.printStackTrace(); 51             } catch(SQLException e) { 52             //數據庫鏈接失敗異常處理
    53  e.printStackTrace(); 54             }catch (Exception e) { 55             // TODO: handle exception
    56  e.printStackTrace(); 57         }finally{ 58             System.out.println("數據庫數據成功獲取!!"); 59  } 60  } 61 
    62 }
相關文章
相關標籤/搜索