posts - 2,  comments - 0,  trackbacks - 0

          好久沒好好凈下心來寫代碼了,習慣了ctrl+c,ctrl+v,發現現在連最簡單的jdbc連接都忘記怎么寫了,感謝beansoft的奉獻,借著熟悉MyEclipse下的開發,好好寫寫代碼.就這么個簡單的例子,也是寫寫停停的.看來還要勤加練習啊.

          package biz;

          import java.sql.*;

          public class JDBCHelloWorld{
              
          public static void main(String[] args){
                  
          try{
                      Class.forName(
          "com.mysql.jdbc.Driver");
                  }
          catch(ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

                  Connection conn 
          = null;
                  Statement stmt 
          = null;
                  ResultSet rs 
          = null;
                  
          try{
                      conn 
          = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&charcterEnconding=GBK","root",null);
                      stmt 
          = conn.createStatement();
                      stmt.executeUpdate(
          "insert into student(username,password,age) values('王五','1234',25)");
                      rs 
          = stmt.executeQuery("select id,username,password,age from student");
                      
          while(rs.next()){
                          System.out.println(
          "姓名=" + rs.getString("username"));
                          System.out.println(
          "密碼=" + rs.getString("password"));
                          System.out.println(
          "年齡=" + rs.getString("age"));
                      }

                  }
          catch(SQLException e){
                      e.printStackTrace();
                  }
          finally{
                      
          try{
                          rs.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                      
          try{
                          stmt.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                      
          try{
                          conn.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }

                  
              }

          }

          posted on 2008-05-14 16:30 hurray 閱讀(740) 評論(0)  編輯  收藏 所屬分類: java學習

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


          網站導航:
           
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宝鸡市| 梨树县| 渭南市| 泽州县| 武邑县| 贵港市| 东乌珠穆沁旗| 玉环县| 都匀市| 宽城| 镇沅| 阜阳市| 浙江省| 玉环县| 随州市| 京山县| 渝中区| 宜州市| 蒲江县| 上饶县| 梅河口市| 鄂托克旗| 新竹市| 剑阁县| 海城市| 天柱县| 宜兴市| 大理市| 二连浩特市| 麦盖提县| 文昌市| 涿州市| 株洲市| 沙河市| 盐边县| 马关县| 佛坪县| 亚东县| 金塔县| 乌苏市| 通州区|