package com.db;
import java.sql.*;
import java.util.*;
public class DBManger implements java.io.Serializable{
private String DB_DRIVER = "com.mysql.jdbc.Driver";
private String ConnStr = "jdbc:mysql://localhost:3306/meter";
private String USERID = "root";
private String USERPWD = "123";
private Connection conn = null;
private Statement stmt=null;
ResultSet rs = null;
//----------------------Database Connection-------------------
public DBManger() throws SQLException {
try {
Class.forName(DB_DRIVER);
conn = DriverManager.getConnection(ConnStr,USERID,USERPWD);
conn.setAutoCommit(false);
}
catch (java.lang.ClassNotFoundException e){
System.err.println("DBManger():" + e.getMessage());
}
catch (SQLException ex){
System.err.println("DBManger.getConnection()" + ex.getMessage());
throw ex;
}
}
//------------------------Closeconn-------------------------
public void closeconn(){
if(conn != null){
try{
conn.close();
}
catch(SQLException ex) {
System.err.println("closeconn()"+ex.getMessage());
}
}
}
//-------------------------closestmt--------------------------
public void closestmt(){
try{
stmt.close();
}
catch(SQLException ex) {
System.err.println("closestmt():"+ex.getMessage());
}
}
//------------------------getConnection----------------------
public Connection getConnection() {
return conn;
}
//------------------------rollback---------------------------
public void rollback() throws SQLException{
conn.rollback();
}
//------------------------setAutoCommit---------------------
public void setAutoCommit(boolean bol) throws SQLException{
try {
conn.setAutoCommit(bol);
}
catch (SQLException ex){
throw ex;
}
}
//--------------------------commit-------------------------
public void commit() throws SQLException{
try{
conn.commit();
}
catch(SQLException e) {
conn.rollback();
throw e;
}
}
//----------------------Database Count--------------------
public int executeCount(String sql)throws SQLException {
Statement stmt = null;
rs = null;
String str = new String();
stmt = conn.createStatement();
try {
rs = stmt.executeQuery( sql );
int i = 0;
while(rs.next()&& (!rs.isAfterLast())){
i += 1;
}
return i;
}
catch ( SQLException ex ) {
System.err.println("DBManger.executeQuery(): " + ex.getMessage());
throw ex;
}
catch(Exception es){
System.out.println("DBManger.executeQuery(): " + es.getMessage());
}
return -1;
}
//----------------------Database Select--------------------
public ResultSet executeQuery( String sql ) throws SQLException{
rs = null;
try{
Statement stmt = conn.createStatement();
rs = stmt.executeQuery( sql );
}
catch ( SQLException ex ){
throw ex;
}
catch ( Exception ex ){
throw new SQLException("");
}
return rs;
}
//----------------------Database Update---------------------
public int executeUpdate( String sql ) throws SQLException{
int count = 0;
try{
Statement stmt = conn.createStatement();
count = stmt.executeUpdate(sql);
}
catch ( SQLException ex ){
throw ex;
}
catch ( Exception ex ){
throw new SQLException("");
}
return count;
}
//----------------------Database Insert--------------------
public boolean executeInsert(String sql)throws SQLException {
int count = 0;
try{
Statement stmt = conn.createStatement();
count = stmt.executeUpdate(sql);
}
catch ( SQLException ex ){
throw ex;
}
catch ( Exception ex ){
throw new SQLException("");
}
if(count != 0 ){
return true;
}
else{
return false;
}
}
}