package com.zhiwei.manage.util;

import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mysql.jdbc.Connection;

/**
 * mysql链接
 * 
 * @author admin
 *
 */
public class MysqlConnection {

	private static final Log log = LogFactory.getLog(MysqlConnection.class);
	private String host;
	private int port;
	private String dbName;

	public MysqlConnection(String host, Integer port, String dbName) {
		this.host = host;
		this.port = port;
		this.dbName = dbName;
	}

	/**
	 * 需要登录的mysql连接
	 * 
	 * @param user
	 * @param password
	 * @return
	 */
	public Connection getMysqlConnect(String user, String password) {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;
			con = (Connection) DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * 无需登录的mysql连接
	 * 
	 * @return
	 */
	public Connection getMysqlConnect() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;
			con = (Connection) DriverManager.getConnection(url);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
}
