JAVA:数据库操作封装
2008-02-23 10:07:34来源:互联网 阅读 ()
package creator.common.db;
import Java.io.InputStream;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
/**
*
* <p>Title: dbBean.java</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author TanBo
* @version 1.0
*/
public class DbBean {
public java.sql.Connection conn = null; //connection object
public ResultSet rs = null; //resultset object
public Statement stmt = null; //statement object
public PreparedStatement prepstmt = null; //preparedstatement object
private String drivers = null; //connection parameter:drivers
private String url = null; //connection parameter:url
private String user = null; //connection parameter:user
private String password = null; //connection parameter:password
private String jndi_name = null; //connection pool parameter:jndi name
/**
* init()
*/
public DbBean(){
try{
//parameter init
drivers = creator.config.ConfigBundle.getString("drivers");
url = creator.config.ConfigBundle.getString("url");
user = creator.config.ConfigBundle.getString("user");
password = creator.config.ConfigBundle.getString("password");
jndi_name = creator.config.ConfigBundle.getString("jndi_name");
//db connection pool init
//InitialContext env = new InitialContext();
//javax.sql.DataSource pool = (javax.sql.DataSource)env.lookup(jndi_name);
//conn = pool.getConnection();
//db connection init
Class.forName(drivers);
conn = DriverManager.getConnection(url,user,password);
//db statement init
stmt = conn.createStatement();
}catch(Exception e){
System.out.println("dbBean: init error!" e.toString());
}
}
/**
* @function prepareStatement
* @param sql String
* @throws SQLException
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* @function prepareStatement
* @param sql String
* @param resultSetType int
* @param resultSetConcurrency int
* @throws SQLException
*/
public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/**
* @function executeQuery
* @param sql String
* @throws SQLException
* @return ResultSet
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}else return null;
}
/**
* @function executeQuery
* @throws SQLException
* @return ResultSet
*/
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}else return null;
}
/**
* @function executeUpdate
* @param sql String
* @throws SQLException
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
* @function executeUpdate
* @throws SQLException
*/
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* @function executeUpdate
* @throws SQLException
*/
public void executeBatch() throws SQLException {
if (prepstmt != null)
prepstmt.executeBatch();
}
/**
* @function addBatch
* @param value String
* @throws SQLException
*/
public void addBatch(String value) throws SQLException {
prepstmt.addBatch(value);
}
/**
* @function setString
* @param index int
* @param value String
* @throws SQLException
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index, value);
}
/**
* @function setInt
* @param index int
* @param value int
* @throws SQLException
*/
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
/**
* @function setBoolean
* @param index int
* @param value boolean
* @throws SQLException
*/
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
/**
* @function setDate
* @param index int
* @param value Date
* @throws SQLException
*/
public void setDate(int index,Date value) throws SQLException {
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:JSP的运行环境配置方法
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash