JAVA: Database Operations Encapsulation

Source: Internet
Author: User
Tags date commit config rollback stmt
Encapsulation | data | database 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 {
Prepstmt.setdate (Index,value);
}

/**
* @function Setlong
* @param index int
* @param value Long
* @throws SQLException
*/
public void Setlong (int index,long value) throws SQLException {
Prepstmt.setlong (Index,value);
}

/**
* @function SetFloat
* @param index int
* @param value Float
* @throws SQLException
*/
public void setfloat (int index,float value) throws SQLException {
Prepstmt.setfloat (Index,value);
}

/**
* @function SetBytes
* @param index int
* @param value byte[]
* @throws SQLException
*/
public void setbytes (int index,byte[] value) throws sqlexception{
Prepstmt.setbytes (Index,value);
}

/**
* @function Setbinarystream
* @param index int
* @param value InputStream
* @param len Int
* @throws SQLException
*/
public void Setbinarystream (int index,inputstream value,int len) throws sqlexception{
Prepstmt.setbinarystream (Index,value,len);
}

/**
* @function Settimestamp
* @param index int
* @param timestamp timestamp
* @throws SQLException
*/
public void Settimestamp (int index,timestamp Timestamp) throws SQLException {
Prepstmt.settimestamp (index, timestamp);
}

/**
* @function Setautocommit
* @param value Boolean
* @throws SQLException
*/
public void Setautocommit (Boolean value) throws sqlexception{
if (this.conn!= null)
This.conn.setAutoCommit (value);
}

/**
* @function Commit
* @throws SQLException
*/
public void Commit () throws sqlexception{
This.conn.commit ();
}

/**
* @function rollback
* @throws SQLException
*/
public void rollback () throws sqlexception{
This.conn.rollback ();
}

/**
* @function Close
* @throws Exception
*/
public void Close () {
try{
if (Rs!= null) {
Rs.close ();
rs = null;
}
}catch (Exception e) {
System.out.println ("Dbbean close Rs error!");
}finally{
try{
if (stmt!= null) {
Stmt.close ();
stmt = null;
}
}catch (Exception e) {
System.out.println ("Dbbean close stmt error!");
}finally{
try{
if (prepstmt!= null) {
Prepstmt.close ();
prepstmt = null;
}
}catch (Exception e) {
System.out.println ("Dbbean close prepstmt error!");
}finally{
try{
IF (conn!= null) {
Conn.close ();
conn = null;
}
}catch (Exception e) {
System.out.println ("Dbbean close conn error!");
}
}
}
}
}

}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.