1. 程式人生 > 實用技巧 >任務一: 2、JDBC

任務一: 2、JDBC

        
        //完整版的jdbc連線資料庫的程式碼,常用配置檔案實現的
        import java.sql.*;
        import java.util.*;
        
        public class JDBCtest04{
            public static void main(String [] args){
                
                //使用資源繫結器繫結屬性配置檔案
                ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
                String driver 
= dundle.getString("driver"); String url = dundle.getString("url"); String password = dundle.getString("passwod"); Connection conn; Statement stmt; try{ //1,註冊驅動 Class.forname(driver);
//2,獲取連線 conn = driverManager.getConnection(url,user,password); //3,獲取資料庫操作物件 stmt = conn.createStatement(); //4,執行sql語句 String sql = "update dept set dno ='xxx'";
int count = stmt.executeUpdate(sql); //5,如果第四步是查詢的sql語句的話,在執行這個 }catch(SQLException e){ e.printStackTrace(); } finally{ //6,釋放資源,要分別釋放資源,從小到大 if(stmt != null){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } }

我的配置檔名:jdbc.properties

  內容:driver=xxxxx

     url=xxxxx

     user=xxxx

     password=xxxx