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!");
}
}
}
}
}
}