jdbc 连接数据库封装类,可返回map,或者vo(自用)
1.DbConnection类
package com.coffee.databaseservices.database.DBConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.coffee.bean.db.accounts.modelObjectVo;
//import org.apache.log4j.Logger;
public class DbConnection{
// Logger log = Logger.getLogger("log4j");
private Connection dbConnection = null;
private Statement selectPro = null; //用于 select 操作
private Statement updatePro = null; //用于 update 操作
private ResultSet dbResultSet = null; //操作 select 结果集
private PreparedStatement pstmt=null;
private String driverName;//声明MySql驱动类
private String dbHost;
private String dbPort;
private String dbName;
private String dbUserName;
private String dbPassword;
private String enCoding;
private String url;
//private Connection dbResultSet;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Connection getDbConnection() {
return dbConnection;
}
public void setDbConnection(Connection dbConnection) {
this.dbConnection = dbConnection;
}
/** *//**
* 实例化DbConnection对象
* @param host 数据库主机(IP)
* @param port 数据库端口
* @param dName 数据库名称
* @param uName 用户名
* @param password 用户密码
*
*
*/
public DbConnection(String host, String port, String dName, String uName, String password){
driverName = "com.mysql.jdbc.Driver";
dbHost = host;
dbPort = port;
dbName = dName;
dbUserName = uName;
dbPassword = password;
enCoding = "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true";
}//end DbConnection( )
/** *//**
* 实例化DbConnection对象
* @param host 数据库主机(IP)
* @param port 数据库端口
* @param dName 数据库名称
* @param uName 用户名
* @param password 用户密码
*/
public DbConnection(){
Properties prop=new Properties();
InputStream in=this.getClass().getResourceAsStream("/db_sysconfig.properties");
if(in==null){
in = ClassLoader.getSystemResourceAsStream("/db_sysconfig.properties");
}
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// dbHost=prop.getProperty("ADBIP");
//
// dbPort=prop.getProperty("ADBPort");
//
// dbName=prop.getProperty("ADBName");
//
// dbUserName =prop.getProperty("ADBUserName");
//
// dbPassword=prop.getProperty("ADBPassword");
dbHost=prop.getProperty("SDBIP");
dbPort=prop.getProperty("SDBPort");
dbName=prop.getProperty("SDBName");
dbUserName =prop.getProperty("SDBUserName");
dbPassword=prop.getProperty("SDBPassword");
driverName = "com.mysql.jdbc.Driver";
enCoding = "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true";
}
/** *//**
* 连接数据库
* @return 连接成功返回true,连接失败返回false
*/
public boolean dbConnectionSuccess(){
boolean flag=false;
//Connection dbConnection = null;
StringBuilder urlTem = new StringBuilder();
urlTem.append("jdbc:mysql://");
urlTem.append(dbHost);
urlTem.append(":");
urlTem.append(dbPort);
urlTem.append("/");
urlTem.append(dbName);
urlTem.append(enCoding);
String url = urlTem.toString();