使用mysql(6部曲)

來源:互聯網
上載者:User

標籤:style   blog   io   color   os   使用   sp   for   檔案   

 

使用properties檔案設定資料庫驅動,uri,使用者名稱和密碼;以便於日後更換資料庫不用更改原始碼;

driver=com.mysql.jdbc.Driveruri=jdbc:mysql://localhost:3306/day10user=rootpassword=root

 

1.註冊驅動

1 public static Connection getConn() throws FileNotFoundException, IOException, ClassNotFoundException, SQLException{2         3         Class.forName(prop.getProperty("driver"));4         return DriverManager.getConnection(prop.getProperty("uri"),prop.getProperty("user"),prop.getProperty("password"));5         6         7     }

 

2.建立連接器

Connection conn = JDBCUtils.getConn();//Connection conn = DriverMananger("uri,user,password");

 

3.擷取傳輸器

Statment stat = conn.createStatement();

 

5.利用傳輸器查詢語句,並返回結果集

stat.executeUpdate(sql); 增刪改

stat.executeQuery(sql);查詢語句

6.關閉資源

 1 public static void close(Connection conn,Statement stat,ResultSet rs){ 2          3             if(rs!=null){ 4                 try { 5                     rs.close(); 6                 } catch (SQLException e) { 7                     throw new RuntimeException(e); 8                      9                 }finally{10                     rs=null;11                 }12             }13             if(stat!=null){14                 try {15                     stat.close();16                 } catch (SQLException e) {17                     throw new RuntimeException(e);18                     19                 }finally{20                     stat=null;21                 }22             }23             if(conn!=null){24                 try {25                     conn.close();26                 } catch (SQLException e) {27                     throw new RuntimeException(e);28                     29                 }finally{30                     conn=null;31                 }32             }33     }

 

使用mysql(6部曲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.