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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          隨筆分類

          隨筆檔案(1)

          收藏夾(18)

          搜索

          •  

          最新評論

          java jdbc

          Posted on 2008-02-14 21:35 semovy 閱讀(1133) 評論(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("向數據庫插入一萬條記錄用去:" +  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 反向控制.
            // 發明者新命名為:DI dipendency Injection 依賴注入.
            // 反向控制是實現依賴抽象,不是抽象依賴實現.
            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();
           }

          }

          主站蜘蛛池模板: 贵定县| 永善县| 乐清市| 乐山市| 容城县| 乌恰县| 宁武县| 都兰县| 壤塘县| 广灵县| 花垣县| 扎赉特旗| 左权县| 东源县| 宜州市| 屯留县| 监利县| 顺义区| 新安县| 元阳县| 兰西县| 巢湖市| 澳门| 灵台县| 普宁市| 苏尼特右旗| 姚安县| 渝中区| 德格县| 蒙城县| 静海县| 格尔木市| 白城市| 五台县| 崇信县| 绥棱县| 建瓯市| 宜兰县| 北海市| 萨嘎县| 定边县|