package org.idcn.util
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import javax.sql.DataSource;
public class DBUtil {
private Connection conn = null;
private PreparedStatement prepStmt = null;
private DBUtil(Connection conn, String sql) throws SQLException {
this.conn = conn;
prepStmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
private DBUtil(DataSource ds, String sql) throws SQLException {
conn = ds.getConnection();
prepStmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
public static DBUtil getInstance(Connection connection, String sql) throws SQLException {
return new DBUtil(connection, sql);
}
public static DBUtil getInstance(DataSource dataSource, String sql) throws SQLException {
return new DBUtil(dataSource, sql);
}
public Connection getConnection() {
return conn;
}
public PreparedStatement getPreparedStatement() {
return prepStmt;
}
public ResultSet executeQuery() throws SQLException {
return prepStmt.executeQuery();
}
public int executeUpdate() throws SQLException {
if(prepStmt == null)
return 0;
return prepStmt.executeUpdate();
}
public void close() {
try {
if (prepStmt != null) {
prepStmt.close();
prepStmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
}
}
public void setString(int index,String value) throws SQLException {
prepStmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException {
prepStmt.setInt(index,value);
}
public void setDouble(int index,Double value) throws SQLException {
prepStmt.setDouble(index, value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepStmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepStmt.setDate(index,value);
}
public void setTime(int index,Time value) throws SQLException {
prepStmt.setTime(index,value);
}
public void setTimestamp(int index,Timestamp value) throws SQLException {
prepStmt.setTimestamp(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepStmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepStmt.setFloat(index,value);
}
public void setObject(int index, Object obj) throws SQLException {
prepStmt.setObject(index, obj);
}
public void setBinaryStream(int index,InputStream in,int length) throws SQLException {
prepStmt.setBinaryStream(index,in,length);
}
public void commit() {
try {
conn.commit();
} catch(Exception e) {
e.printStackTrace();
}
}
public void rollback() {
try {
conn.rollback();
}
catch(Exception e) {
e.printStackTrace();
}
}
}