Java连接数据库类DBSource

import java.io.InputStream;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

//import com.vfrankmusic.sysinfo.SysInfo;

/**
 * <p>Title: MusicWeb</p>
 * <p>Description: MusicWeb site</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: VFrankSoft</p>
 * @author 乌小龙
 * @version 1.0
 */

public class DBSource {
 public Connection conn = null;
 public ResultSet rs = null;
 public Statement stmt = null;
 public PreparedStatement pstmt = null;
 public DataSource ds = null;
 private static DBSource instance;
 
 private String drivers = null;
 private String url = null;
 private String user = null;
 private String password = null;
 private String jndi = null;
 
 public synchronized static DBSource getInstance() {
  if (instance == null) {
   instance = new DBSource();
  }
  return instance;
 }
 
 private DBSource() {
  try {
   InitialContext ctx = new InitialContext();
   //ds = (DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");
   ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
   
   conn = ds.getConnection();
   stmt = conn.createStatement();
  } catch(Exception ex) {
   System.out.println("出现例外,信息是:" + ex.getMessage());
   ex.printStackTrace();
  }
 }
 
 /**
  * @function prepareStatement
  * @param sql
  * @throws SQLException
  */
 public void prepareStatement(String sql) throws SQLException {
  pstmt = conn.prepareStatement(sql);
 }
 
 /**
  *
  * @param sql
  * @param resultSetType
  * @param resultSetConcurrency
  * @throws SQLException
  */
 public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
         throws SQLException {
  pstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
 }
 
 //查询
 /**
  *
  * @param sql
  * @return
  * @throws SQLException
  */
 public ResultSet executeQuery(String sql) throws SQLException {
  if (stmt != null) {
   return stmt.executeQuery(sql);
  } else
   return null;
 }
 
 /**
  *
  * @return
  * @throws SQLException
  */
 public ResultSet executeQuery() throws SQLException {
  if (pstmt != null) {
   return pstmt.executeQuery();
  } else
   return null;
 }
 
 //更新
 /**
  *
  */
 public void executeUpdate(String sql) throws SQLException {
  if (stmt != null) {
   stmt.executeUpdate(sql);
  }
 }

 /**
  * @throws SQLException
  */
 public int executeUpdate() throws SQLException {
  int value = 0;
  if (pstmt != null) {
   value = pstmt.executeUpdate();
  }
  return value;
 }
 
 //
 /**
  *
  * @throws SQLException
  */
 public void executeBatch() throws SQLException {
  if (pstmt != null)
   pstmt.executeBatch();
 }
 
 /**
  *
  * @param value
  * @throws SQLException
  */
 public void addBatch(String value) throws SQLException {
  pstmt.addBatch();
 }
 
 //
 public void setString(int index, String value) throws SQLException {
  pstmt.setString(index, value);
 }
 
 public void setInt(int index, int value) throws SQLException {
  pstmt.setInt(index, value);
 }
 
 public void setBoolean(int index, boolean value) throws SQLException {
  pstmt.setBoolean(index, value);
 }
 
 public void setDate(int index, Date value) throws SQLException {
  pstmt.setDate(index, value);
 }
 
 public void setLong(int index, long value) throws SQLException {
  pstmt.setLong(index, value);
 }
 
 public void setFloat(int index, float value) throws SQLException {
  pstmt.setFloat(index, value);
 }
 
 public void setBeytes(int index, byte[] value) throws SQLException {
  pstmt.setBytes(index, value);
 }
 
 public void setBinaryStream(int index, InputStream value, int len) throws SQLException {
  pstmt.setBinaryStream(index, value, len);
 }
 
 public void setTimestamp(int index, Timestamp timestamp) throws SQLException {
  pstmt.setTimestamp(index, timestamp);
 }
 
 //事务
 public void setAutoCommit(boolean value) throws SQLException {
  if (this.conn != null)
   this.conn.setAutoCommit(value);
 }
 
 public void commit() throws SQLException {
  this.conn.commit();
 }
 
 public void roolback()throws SQLException {
  this.conn.rollback();
 }
 
 public void close() {
  try {
   if (rs != null) {
    rs.close();
    rs = null;
   }
  } catch(Exception ex) {
   System.out.println("DataBase Source close rs error!");
  } finally {
   try {
    if (stmt != null) {
     stmt.close();
     stmt = null;
    }
   } catch(Exception ex) {
    System.out.println("DataBase Source close stmt error!");
   } finally {
    try {
     if (pstmt != null) {
      pstmt.close();
      pstmt = null;
     }
    } catch(Exception ex) {
     System.out.println("DataBase Source close pstmt error!");
    } finally {
     try {
      if (conn != null) {
       conn.close();
       conn = null;
      }
     } catch(Exception ex) {
      System.out.println("DataBase Source close conn error!");
     }
    }
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值