<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 閱讀(1130) 評論(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 依賴注入.
      // 反向控制是實(shí)現(xiàn)依賴抽象,不是抽象依賴實(shí)現(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综合色区无码一二三区 | 国产成人高清亚洲一区91| 免费国产不卡午夜福在线| 成人影片一区免费观看| 一级毛片**免费看试看20分钟 | 亚洲日韩在线中文字幕综合 | 精品亚洲综合在线第一区| 免费三级毛片电影片| 一级a性色生活片久久无少妇一级婬片免费放| 在线亚洲人成电影网站色www| 综合在线免费视频| 一级毛片免费毛片毛片| 亚洲一区二区三区播放在线| 亚洲一级特黄大片无码毛片| av无码免费一区二区三区| 九九99热免费最新版| 亚洲国产日韩综合久久精品| 亚洲色偷偷狠狠综合网| 97在线观免费视频观看| 国产午夜精品免费一区二区三区 | 亚洲成人免费在线观看| 一级毛片成人免费看a| 亚洲精品美女网站| 久久夜色精品国产嚕嚕亚洲av| 日韩高清免费观看| 在线成人爽a毛片免费软件| 一本大道一卡二大卡三卡免费| youjizz亚洲| 久久精品国产亚洲AV嫖农村妇女| 亚洲精品老司机在线观看| 巨胸喷奶水视频www网免费| 无码AV片在线观看免费| 国产精品小视频免费无限app| 亚洲欧美不卡高清在线| 亚洲午夜久久久精品电影院| 亚洲AV无码成人精品区在线观看| 亚洲精品国产电影| 国产一级一片免费播放| 好男人看视频免费2019中文 | 国产成人精品日本亚洲专区| 国产在线98福利播放视频免费|