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) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導航

          統計

          公告

          求知若饑,虛心若愚

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評論

          主站蜘蛛池模板: 江达县| 江津市| 渭源县| 武强县| 舒兰市| 兴海县| 宁都县| 蓬安县| 论坛| 宁津县| 丰顺县| 辛集市| 昆山市| 金山区| 修武县| 潮州市| 湘乡市| 德格县| 册亨县| 陆河县| 浮梁县| 法库县| 吉木乃县| 南城县| 怀安县| 泾源县| 浦县| 岳池县| 临安市| 马龙县| 奉贤区| 嘉黎县| 吉林省| 大理市| 汾西县| 孝昌县| 云阳县| 西贡区| 图片| 无锡市| 宝山区|