数据库连接 例子

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值