jdbc

           1 import java.io.IOException;
           2 import java.io.InputStream;
           3 import java.sql.Connection;
           4 import java.sql.DriverManager;
           5 import java.sql.ResultSet;
           6 import java.sql.Statement;
           7 import java.util.Properties;
           8 
           9 public class JdbcMySql {
          10 
          11     public static Properties getProperties() {
          12         Properties props = new Properties();
          13         InputStream is = null;
          14         try {
          15             is = JdbcMySql.class.getResourceAsStream("/jdbc_mysql.properties");
          16             props.load(is);
          17         } catch (Exception e) {
          18             e.printStackTrace();
          19             return null;
          20         } finally {
          21             if(is != null
          22                 try {
          23                     is.close();
          24                 } catch (IOException e) {
          25                     e.printStackTrace();
          26                 }
          27         }
          28         return props;
          29     }
          30     
          31     public static void main(String[] args) {
          32         Connection conn = null;
          33         Statement stmt = null;
          34         ResultSet rs = null;
          35         
          36         Properties props = getProperties();
          37         if (props != null) {
          38             String driver = props.getProperty("driver");
          39             String url = props.getProperty("url");
          40             String user = props.getProperty("user");
          41             String password = props.getProperty("password"); 
          42             String characterEncoding = props.getProperty("characterEncoding");
          43             
          44             try {
          45                 Class.forName(driver);
          46                 conn = DriverManager.getConnection(url + "?characterEncoding=" + characterEncoding, user, password);
          47                 stmt = conn.createStatement();
          48                 
          49                 String sql = "delete from student where id = '2'";
          50                 stmt.executeUpdate(sql);
          51                 
          52                 sql = "select * from student";
          53                 rs = stmt.executeQuery(sql);
          54                 
          55                 while (rs.next()) {
          56                     System.out.print(rs.getInt(1+ "\t");
          57                     System.out.print(rs.getString(2+ "\t");
          58                     System.out.print(rs.getString(3+ "\n");
          59                 }
          60             } catch (Exception e) {
          61                 e.printStackTrace();
          62             } finally {
          63                 try {
          64                     if (rs != null
          65                         rs.close();
          66                     if (stmt != null
          67                         stmt.close();
          68                     if (conn != null)
          69                         conn.close();
          70                 } catch (Exception ex) {
          71                     ex.printStackTrace();
          72                 }
          73             }
          74         }
          75     }
          76 }

          posted on 2009-04-03 20:32 flynewton 閱讀(62) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          求知若饑,虛心若愚

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 锡林浩特市| 昌江| 济宁市| 汶上县| 江油市| 平湖市| 仁怀市| 包头市| 临洮县| 汾阳市| 周至县| 清水河县| 东山县| 马龙县| 嘉义县| 新河县| 商丘市| 乌审旗| 重庆市| 石首市| 华安县| 阜阳市| 临猗县| 肇州县| 白银市| 米脂县| 勐海县| 呼伦贝尔市| 江川县| 耿马| 囊谦县| 阳高县| 巴里| 大港区| 龙川县| 阿拉善盟| 苏尼特左旗| 东方市| 射阳县| 达州市| 平顶山市|