2009年4月3日

          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 @ 2009-04-03 20:32 flynewton 閱讀(62) | 評論 (0)編輯 收藏

          僅列出標題  
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          求知若饑,虛心若愚

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評論

          主站蜘蛛池模板: 芜湖县| 南召县| 锦州市| 曲靖市| 田阳县| 丰顺县| 饶河县| 德钦县| 七台河市| 崇仁县| 普格县| 禄丰县| 怀来县| 临邑县| 西安市| 泽库县| 赤峰市| 沈阳市| 浑源县| 娱乐| 南投县| 湾仔区| 紫阳县| 岑溪市| 恩施市| 廊坊市| 澳门| 芦溪县| 平邑县| 乌拉特中旗| 确山县| 吉木乃县| 贵南县| 平湖市| 重庆市| 东乌| 东明县| 南丹县| 广南县| 巴楚县| 山阴县|