One class of the Java linked database

Source: Internet
Author: User
Tags bool commit getmessage rollback stmt create database

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 ();
}
}

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.