Package cn.baiweigang.qtaf.toolkit.mysql

Source Code of cn.baiweigang.qtaf.toolkit.mysql.ConnMysql

package cn.baiweigang.qtaf.toolkit.mysql;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.baiweigang.qtaf.toolkit.util.LogUtil;

import com.mysql.jdbc.Statement;

/**
* 说明:连接数据库配置信息
* @author bwgang 2013-01-08
*
*/
public class ConnMysql {
 
  private static Connection con = null//创建用于连接数据库的Connection对象 
  private static LogUtil log=LogUtil.getLogger(ConnMysql.class);//日志记录 
 
  public  String ip="127.0.0.1";
  public  String port="3306";
  public  String dataname="";
  public  String username="root";
  public  String password="";
 
  public ConnMysql(){
   
  }

  public ConnMysql(String ip,String port,String dataname,String username,String password){
    this.ip=ip;
    this.port=port;
    this.dataname=dataname;
    this.username=username;
    this.password=password;
  }
 
  /**
   * 说明:数据库连接信息,并创建数据库连接
   * @return
   */
public   Connection getConnection() {   
        try
          //如果连接未释放,先断开连接
          if(con != null) {
//            con.close();
//            con=null;
//            log.info("手动关闭数据库连接成功");
            return con;//未关闭时,不再重新连接,直接返回
          }
          Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动  
          con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+dataname+"?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=round", username, password);// 创建数据连接 
        } catch (Exception e) { 
          log.error("数据库连接失败");
          log.error( e.getMessage());
       
    return con; //返回所建立的数据库连接  


/**
* 说明:关闭数据库连接
*/
public  void closedConn() { 
 
  try{
    if(con != null) con.close();
    con=null;
    log.info("关闭数据库连接成功【"+this.ip+"_"+this.port+"_"+this.dataname+"】");
    } catch(SQLException e){
      log.error("关闭数据库连接失败【"+this.ip+"_"+this.port+"_"+this.dataname+"】");
      log.error(e.getMessage());
    }
  } 
 
/**
* 说明:用于执行插入、更新、删除的sql语句,当受影响的行数为0和执行失败时返回false
* @param sql
* @return 成功返回true,失败返回false
*/
  public  boolean excSql(String sql) { 
  if(con == null)getConnection(); //连接到数据库  
 
     try
        Statement st = (Statement) con.createStatement();    // 创建用于执行静态sql语句的Statement对象  
           int counts=st.executeUpdate(sql)// 执行操作的sql语句  
           if (0==counts) {
          log.info("执行成功,,共0条数据受到影响,没有完成操作!:SQL语句-->【"+sql+"】");
      return false;
           }
           log.info("执行成功,共"+counts+"条数据受到影响:"+"SQL语句-->【"+sql+"】");
           return true;
       } catch (SQLException e) { 
         log.error("执行失败:SQL语句-->【"+sql+"】");
         log.error(e.getMessage());
           return false;
     
    
  } 

  /**
   * 说明:用户select的查询语句,返回查询结果集
   * @param sql
   * @return 查询结果集
   */
  public  ResultSet querySql(String sql) { 
         
      ResultSet rs=null;
      if(con == null)getConnection(); //连接到数据库  
          try
            Statement st = null;   //创建用于执行静态sql语句的Statement对象,st属局部变量  
            st=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
              rs = st.executeQuery(sql);    //执行sql查询语句,返回查询数据的结果集              
              log.info("执行成功:SQL查询语句-->【"+sql+"】");
          }       
            catch (SQLException e) { 
              log.error("执行失败:SQL查询语句-->【"+sql+"】");
              log.error(e.getMessage());
             rs=null;
            }
            return rs;
     
   
 
 
}


TOP

Related Classes of cn.baiweigang.qtaf.toolkit.mysql.ConnMysql

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.