Package com.pmjava.jdbcdb;
Import Java.io.PrintStream;
Import java.sql.*;
Import java.util.Properties;
Import Javax.sql.DataSource;
public class Dbconnect
{
private static DataSource DS;
Private Connection Conn;
Private Statement stmt;
Private PreparedStatement prepstmt;
Private ResultSet RS;
Private String Dbdriver;
Private String Dburl;
Private String Dbuser;
Private String Dbpassword;
Private Dbconnect ()
{
conn = null;
stmt = null;
prepstmt = null;
rs = null;
Dbdriver = "";
Dburl = "";
Dbuser = "";
Dbpassword = "";
Try
{
Init ();
Try
{
Class.forName (Dbdriver);
}
catch (ClassNotFoundException e)
{
SYSTEM.OUT.PRINTLN ("Boot database driver error:" + e.getmessage ());
}
conn = Drivermanager.getconnection (Dburl, Dbuser, Dbpassword);
Conn.setautocommit (TRUE);
stmt = Conn.createstatement ();
}
catch (SQLException e)
{
SYSTEM.OUT.PRINTLN ("CREATE Database connection Error:" + e.getmessage ());
}
}
private void Init ()
{
Java.io.InputStream is = GetClass (). getResourceAsStream ("/database.properties");
Properties Dbprops = new properties ();
Try
{
Dbprops.load (IS);
Dbdriver = Dbprops.getproperty ("Driver", "com.mysql.jdbc.Driver");
Dburl = dbprops.getproperty ("url", "jdbc:mysql://localhost/db?") AUTORECONNECT=TRUE&DEFAULTAUTOCOMMIT=FALSE&USEUNICODE=TRUE&CHARACTERENCODING=GBK ");
Dbuser = Dbprops.getproperty ("username", "1111111");
Dbpassword = Dbprops.getproperty ("Password", "111111");
}
catch (Exception e)
{
SYSTEM.ERR.PRINTLN ("Cannot read property file: Make sure Database.properties is in classpath specified path");
Return
}
}
public static Dbconnect getinstance ()
{
return new Dbconnect ();
}
Public DataSource Getdatasource ()
{
return DS;
}
Public Connection getconnection ()
{
Return conn;
}
public void Setautocommit (boolean bool)
Throws SQLException
{
Conn.setautocommit (BOOL);
}
public void preparestatement (String sql)
Throws SQLException
{
Clearparameters ();
prepstmt = conn.preparestatement (sql, 1004, 1008);
}
Public ResultSet Executeprepquery ()
Throws SQLException
{
if (prepstmt!= null)
return Prepstmt.executequery ();
Else
return null;
}
public int executeprepupdate ()
Throws SQLException
{
if (prepstmt!= null)
return Prepstmt.executeupdate ();
Else
return 0;
}
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 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 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 setbytes (int index, byte value[])
Throws SQLException
{
Prepstmt.setbytes (index, value);
}
public void Setinteger (int index, Integer value)
Throws SQLException
{
Prepstmt.setlong (Index, Value.longvalue ());
}
public void Setshort (int index, short value)
Throws SQLException
{
Prepstmt.setshort (index, value);
}
private void Clearparameters ()
{
Try
{
Prepstmt.clearparameters ();
Prepstmt.close ();
prepstmt = null;
}
catch (Exception Exception) {}
}
private void Clearstmt ()
{
Try
{
Stmt.close ();
stmt = null;
}
catch (Exception Exception) {}
}
public int executeupdate (String sql)
Throws SQLException
{
Clearstmt ();
if (stmt = null)
stmt = Conn.createstatement ();
return stmt.executeupdate (SQL);
}
Public ResultSet executequery (String sql)
Throws SQLException
{
Clearstmt ();
if (stmt = null)
stmt = Conn.createstatement (1004, 1008);
return stmt.executequery (SQL);
}
public void commit ()
Throws SQLException
{
Conn.commit ();
}
public void rollback ()
Throws SQLException
{
Conn.rollback ();
}
public void Close ()
{
Try
{
if (stmt!= null)
{
Stmt.close ();
stmt = null;
}
if (prepstmt!= null)
{
Prepstmt.clearparameters ();
Prepstmt.close ();
prepstmt = null;
}
IF (conn!= null)
Conn.close ();
}
catch (SQLException ex)
{
SYSTEM.OUT.PRINTLN ("Database connection Shutdown Error:" + ex.getmessage ());
}
}
public static void Main (String args[])
{
Dbconnect DBConnect1 = new Dbconnect ();
}
}