posts - 2,  comments - 0,  trackbacks - 0

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

          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) 評(píng)論(0)  編輯  收藏 所屬分類: java學(xué)習(xí)

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 周口市| 十堰市| 五河县| 武义县| 蒙山县| 工布江达县| 松潘县| 舞阳县| 宜兰市| 孙吴县| 太仆寺旗| 屏山县| 汤原县| 磐安县| 怀远县| 磴口县| 铜陵市| 瓦房店市| 南通市| 厦门市| 民县| 耒阳市| 芜湖县| 晋州市| 扎囊县| 临邑县| 邢台县| 安塞县| 长岛县| 平定县| 紫金县| 罗甸县| 尤溪县| 泸定县| 台北县| 二连浩特市| 高唐县| 浦北县| 桃源县| 府谷县| 汉中市|