posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java jdbc

Posted on 2008-02-14 21:35 semovy 阅读(1123) 评论(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();
 }

}


只有注册用户登录后才能发表评论。


网站导航: