Java jdbc抽象实现类实例


package com.loong.mail.crontab;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 数据库连接
* @author lining
*
*/
public abstract class BaseDaoImpl {
private static final Log log = LogFactory.getLog(BaseDaoImpl.class);

public Connection con = null;

private Statement stm = null;

static {
   try {
    Class.forName("com.mysql.jdbc.Driver");
    //Class.forName("Oracle.jdbc.driver.OracleDriver");
   } catch (ClassNotFoundException e) {
    log.error(e);
   }
}

public BaseDaoImpl() {
   getConnection();
}

public void getConnection() {
//   Connection conn = null;
   try {
   Context ctx = new InitialContext();
   DataSource ds = (DataSource) ctx .lookup("java:comp/env/jdbc/mysql");
   con = ds.getConnection();
   } catch (Exception e) {
   e.printStackTrace();
   }
   }


/*public void getConnection() {
   try {
  
//    con = DriverManager.getConnection(
//      "jdbc:mysql://192.168.100.21:3306/volume?user=webbycmc&password=Root123");
    con = DriverManager.getConnection(
    "jdbc:mysql://192.168.5.74:3306/volume?user=root&password=123456");
    System.out.println("连接成功"+con);
   } catch (SQLException e) {
    e.printStackTrace();
    log.error(e);
   }
   log.info("连接数据库成功");
}
*/
public int update(String sql) {
   int result = 0;
   PreparedStatement preparedStatement;
   try {
    preparedStatement = con.prepareStatement(sql);
    result = preparedStatement.executeUpdate();
   } catch (SQLException e) {
    log.error(e);
   }
   return result;
}

public ResultSet executeQuery(Connection con, String sql) {
   ResultSet result = null;
   PreparedStatement preparedStatement;
   try {
    preparedStatement = con.prepareStatement(sql);
    result = preparedStatement.executeQuery();
   } catch (SQLException e) {
    log.error(e);
   }
   return result;
}

public ResultSet executeQuery(String sql) {
   ResultSet result = null;
   PreparedStatement preparedStatement;
   try {
    preparedStatement = con.prepareStatement(sql);
    result = preparedStatement.executeQuery();
   } catch (SQLException e) {
    log.error(e);
   }
   return result;
}

public void createStatement() {
   try {
    stm = con.createStatement();
   } catch (SQLException e) {
   }
}

private CallableStatement cstmt = null;

public void createCallStatement(String sql) {

   try {
    cstmt = con.prepareCall(sql);
   } catch (SQLException e) {
    log.error(e);
   }
}

public void execute(String sql) {
   if (cstmt != null) {
    try {
     if (sql != null) {
      cstmt.execute(sql);
     } else {
      cstmt.execute();
     }
    } catch (SQLException e) {
     log.error(e);
    }
   }
}

public void addBatch(String sql) {
   if (stm != null) {
    try {
     stm.addBatch(sql);
    } catch (SQLException e) {
    }
   }
}

public void executeBatch() {
   if (stm != null) {
    try {
     stm.executeBatch();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
}

public int getTotalCount(String sql) {
   int totalCount = 0;
   ResultSet result = null;
   PreparedStatement preparedStatement;
   try {
    preparedStatement = con.prepareStatement(sql);
    result = preparedStatement.executeQuery();
    if (result.next()) {
     totalCount = result.getInt(1);
    }
   } catch (SQLException e) {
    log.error(e);
   } finally {
    closeResultSet(result);
   }
   return totalCount;
}

public void disconnect() {
   closeCon(con);
   log.info("断开数据库成功");
}

public void closeCon(Connection con) {
   try {
    if (con != null || !con.isClosed()) {
     con.close();
    }
   } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
}

public void closeResultSet(ResultSet res) {
   if (res != null) {
    try {
     res.close();
    } catch (SQLException e) {
     // TODO 自动生成 catch 块
     e.printStackTrace();
    }
   }
}

public Connection getCon() {
   return con;
}

public void setCon(Connection con) {
   this.con = con;
}
}

相关内容