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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 芷江| 邵武市| 天长市| 武安市| 固阳县| 平罗县| 阿拉善左旗| 寿光市| 望谟县| 林口县| 和龙市| 汽车| 象山县| 英德市| 陇川县| 紫阳县| 犍为县| 万宁市| 兴宁市| 会昌县| 梅河口市| 巩留县| 察雅县| 永寿县| 南召县| 梨树县| 和田市| 洪湖市| 长治市| 宁明县| 务川| 昆山市| 金堂县| 中阳县| 从江县| 镇安县| 清原| 伊金霍洛旗| 枣阳市| 台山市| 长岛县|