<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0, comments - 77, trackbacks - 0, articles - 356
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    java jdbc

    Posted on 2008-02-14 21:35 semovy 閱讀(1131) 評論(0)  編輯  收藏 所屬分類: JDBC

    package com.semovy.service.impl;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.List;

    import com.semovy.bean.Database;

    public class SQLServerDatabase implements Database {

     private String driver = null;
     private String pwd = null;
     private String user = null;
     private String url = null;
     private Connection connection = null;
     public String getUrl() {
      return url;
     }
     public void setUrl(String url) {
      this.url = url;
     }
     public String getUser() {
      return user;
     }
     public void setUser(String user) {
      this.user = user;
     }
     public String getDriver() {
      return driver;
     }
     public String getPwd() {
      return pwd;
     }
     public void setDriver(String driver)
     {
      this.driver = driver;
     }
     public void setPwd(String pwd)
     {
      this.pwd = pwd;
     }
     public Connection getConnection()
     {
      openConnection();
      return this.connection;
     }
     public void setConnection(Connection connection) {
      this.connection = connection;
     }
     public List getData() {
      
      return null;
     }
     public SQLServerDatabase(String driver,String url,String user,String pwd)
     {
      this.driver = driver;
      this.url = url;
      this.user = user;
      this.pwd = pwd;
      try
      {
       System.out.println("start");
       Class.forName(this.getDriver()).newInstance();
       this.connection = DriverManager.getConnection(this.getUrl(), this.getUser(), this.getPwd());
       System.out.println("OK");
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
      catch(ClassNotFoundException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
      catch(Exception e)
      {
       System.out.println(e.getLocalizedMessage());
      }
     }
     /**
      *
      * @return
      */
     private Connection openConnection()
     {
      if(this.connection != null)
      { 
       return this.connection;
      } 
      else
      {
       try
       {
        Class.forName(this.getDriver()).newInstance();
        this.connection = DriverManager.getConnection(this.getUrl(), this.getUser(), this.getPwd());
       }catch(SQLException e)
       {
        System.out.println(e.getLocalizedMessage());
       }
       catch(ClassNotFoundException e)
       {
        System.out.println(e.getLocalizedMessage());
       }
       catch(Exception e)
       {
        System.out.println(e.getLocalizedMessage());
       }
       return this.connection; 
      }
      
     }
     public void closeConnection()
     {
      try
      {
       if(this.getConnection() != null && !this.connection.isClosed())
       {
        this.connection.close();
       }
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
     }
     public ResultSet executeQuery(String sqlExp)
     {
      ResultSet rs = null;
      Statement stmt = null;
      try
      {
       stmt = this.getConnection().createStatement();
       rs = stmt.executeQuery(sqlExp);
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
      return rs;
     }
     public void executeUpdate(String sqlExp)
     {
      try
      {
       this.getConnection().createStatement().executeUpdate(sqlExp);
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
     }
     public void executeBatchUpdate()
     {
      try
      {
       long start = System.currentTimeMillis();
       Statement stmt = this.getConnection().createStatement();
       for(int i=0;i < 10000;i++)
       {
        stmt.addBatch("insert into item values(2,'item" + i + "')");
        if(i%500 == 0 && i>0)
         stmt.executeBatch();
       }
       stmt.executeBatch();
       this.getConnection().commit();
       stmt.close();
       stmt = null;
       long spendTime = System.currentTimeMillis() - start;
       System.out.println("向數(shù)據(jù)庫插入一萬條記錄用去:" +  spendTime*1.0/1000 + " s");
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
     }
     public void display(ResultSet rs)
     {
      try
      {
       ResultSetMetaData rsmd =  rs.getMetaData();
       int colLen = rsmd.getColumnCount();
       while(rs.next())
       {
        for(int i=0; i<colLen;i++)
        {
         System.out.print(rsmd.getColumnName(i+1) + ": " + rs.getObject(i+1) + "  ");
        }
        System.out.println();
       }
       
      }catch(SQLException e)
      {
       System.out.println(e.getLocalizedMessage());
      }
      
     }
    }



    package com.semovy.test;

    import com.semovy.service.impl.SQLServerDatabase;

    public class TestHelloWorld {

     public static void main(String[] args) {

      // ApplicationContext appContext = new
      // FileSystemXmlApplicationContext("classpath:config.xml");
      // Hello hello = (Hello)appContext.getBean("hello");
      // hello.saySaluation();
      // IoC Inversion of Control 反向控制.
      // 發(fā)明者新命名為:DI dipendency Injection 依賴注入.
      // 反向控制是實現(xiàn)依賴抽象,不是抽象依賴實現(xiàn).
      SQLServerDatabase sqlServerDB = new SQLServerDatabase(
        "com.microsoft.jdbc.sqlserver.SQLServerDriver", "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test",
        "sa", "1234");
      sqlServerDB.executeUpdate("drop table item if exist create table item(id int not null identity(1,1) primary key,int categoryId not null,itemName varchar(255) default '')");
      sqlServerDB.executeUpdate("delete from item");
      sqlServerDB.executeBatchUpdate();
      sqlServerDB.display(sqlServerDB.executeQuery("select top 100 * from item"));
      sqlServerDB.closeConnection();
     }

    }

    主站蜘蛛池模板: 亚洲成av人片不卡无码久久| 免费高清资源黄网站在线观看| 亚洲av极品无码专区在线观看| 亚洲国产香蕉人人爽成AV片久久 | 午夜老司机永久免费看片| 亚洲国产成人综合精品| 亚洲精品在线不卡| 亚洲阿v天堂在线| 亚洲人成电影在线播放| 国产免费私拍一区二区三区| 美女尿口扒开图片免费| 亚洲Av高清一区二区三区| 亚洲第一中文字幕| 毛片免费在线观看网址| 久久成人免费大片| 三年在线观看免费观看完整版中文| 亚洲1区2区3区精华液| 亚洲AV男人的天堂在线观看| 亚洲第一页在线视频| 婷婷亚洲综合五月天小说| 亚洲一区二区三区自拍公司| 亚洲精品视频在线观看你懂的| 成人免费视频国产| 白白国产永久免费视频| 免费黄色小视频网站| 黄a大片av永久免费| 午夜小视频免费观看| 成人午夜性A级毛片免费| 成人黄页网站免费观看大全| 色婷婷7777免费视频在线观看 | 青青青免费国产在线视频小草| 国产偷国产偷亚洲清高APP| 亚洲熟女综合色一区二区三区| 亚洲人成7777| 亚洲欧洲另类春色校园网站| 亚洲综合av一区二区三区不卡| 亚洲精品无码成人片久久不卡| 一本色道久久88—综合亚洲精品| 亚洲妇女无套内射精| 国产成人亚洲毛片| 一级做a爰片久久毛片免费看 |