Java Connection Database class dbsource__c#

Source: Internet
Author: User

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 Ouxiaoron
* @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 ("With exception, the message is:" + 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);
}

Inquire
/**
*
* @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;
}

Update
/**
*
*/
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);
}

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

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.