mengkuku

          [轉]注冊jdbc驅動程序的三種方式

           
          1、比較常用
          try{
                 Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動
                 String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
                 Connection conn=DriverManager.getConnection(url,"username","password");
                 Statement stmt=conn.createStatement();
                 ResultSet rs=stmt.executeQuery("select * from tablename");
                 while(rs.next()){//不斷指向下一條記錄
                      System.out.println("DeptNo:"+rs.getInt(1));
                      System.out.println("\tDeptName:"+rs.getString(2));
                      System.out.println("\tLOC:"+rs.getString(3));
          }         
              rs.close();
              stmt.close();
              conn.close();
          }catch(ClassNotFoundException e){
             System.out.println("找不到指定的驅動程序類!");
          }catch(SQLException e){
              e.printStackTrace();
          }


          2、通過系統的屬性設置
          try{
                 System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//系統屬性指定數據庫驅動
                 String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
                 Connection conn=DriverManager.getConnection(url,"username","password");
                 Statement stmt=conn.createStatement();
                 ResultSet rs=stmt.executeQuery("select * from tablename");
                 while(rs.next()){//不斷指向下一條記錄
                      System.out.println("DeptNo:"+rs.getInt(1));
                      System.out.println("\tDeptName:"+rs.getString(2));
                      System.out.println("\tLOC:"+rs.getString(3));
          }         
              rs.close();
              stmt.close();
              conn.close();
          }catch(SQLException e){
              e.printStackTrace();
          }

          3、看起來比較直觀的一種方式,注冊相應的db的jdbc驅動,3在編譯時需要導入對應的lib
          try{
                 new com.mysql.jdbc.Driver();//創建driver對象,加載數據庫驅動
                 String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
                 Connection conn=DriverManager.getConnection(url,"username","password");
                 Statement stmt=conn.createStatement();
                 ResultSet rs=stmt.executeQuery("select * from tablename");
                 while(rs.next()){//不斷指向下一條記錄
                      System.out.println("DeptNo:"+rs.getInt(1));
                      System.out.println("\tDeptName:"+rs.getString(2));
                      System.out.println("\tLOC:"+rs.getString(3));
          }         
              rs.close();
              stmt.close();
              conn.close();
          }catch(SQLException e){
              e.printStackTrace();
          }

          posted on 2012-10-16 19:34 上帝也瘋狂 閱讀(191) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 沁阳市| 蒙城县| 周宁县| 宜宾市| 定安县| 九江县| 贞丰县| 景宁| 锡林郭勒盟| 阿尔山市| 秭归县| 吴川市| 松潘县| 永德县| 区。| 泽库县| 成都市| 丰顺县| 清丰县| 恩平市| 元谋县| 蚌埠市| 东兰县| 若羌县| 岳普湖县| 灵台县| 普安县| 肇州县| 安多县| 东源县| 房产| 岳阳市| 柏乡县| 淅川县| 平乡县| 余江县| 渑池县| 恩施市| 乌鲁木齐县| 金山区| 修水县|