標籤:多個 print while 查詢 uid 對象 return exe 指定
- 下載驅動包:http://dev.mysql.com/downloads/connector/j/,解壓得到jar檔案,例如mysql-connector-java-8.0.11.jar
- 在項目下建立檔案夾,將jar包放進去,點擊項目右鍵,【構建路徑】——【配置構建路徑】,添加jar檔案
- 串連資料庫
- 加粗部分為新特性
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";//指定要訪問的資料庫store11 12 //已載入完驅動13 try14 {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 try13 {14 if (pstmt != null)15 {16 pstmt.close();17 }18 } catch (SQLException e1)19 {20 e1.printStackTrace();21 }22 try23 {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,conn37 */38 public static void queryClose(PreparedStatement pstmt, ResultSet rs, Connection conn)39 {40 try41 {42 if (pstmt != null)43 {44 pstmt.close();45 }46 } catch (SQLException e1)47 {48 e1.printStackTrace();49 }50 try51 {52 if (rs != null )53 {54 rs.close();55 }56 } catch (SQLException e1)57 {58 e1.printStackTrace();59 }60 try61 {62 if (conn != null)63 {64 conn.close();65 }66 } catch (SQLException e)67 {68 e.printStackTrace();69 }70 }71 72 }
測試代碼。測試是否串連成功:
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 exception56 e.printStackTrace();57 }finally{58 System.out.println("資料庫資料成功擷取!!");59 }60 }61 62 }
java JDBC串連mysql