首页>代码>原生java servlet开发企业门户网站 (包含前后端)>/mhwz/src/main/java/com/mingrisoft/DBConnection.java
package com.mingrisoft;			//指定类所在的包

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBConnection					
{

    private String FileName;			//配置文件名
    private int DBType;					//数据库类型
    private String MySqlDriver;			//MYSQL Server驱动程序
    private String MySqlURL; 			//MYSQL Server连接字符串
	private String username;			//用户名
	private String password;			//密码

	public Connection conn = null;	//数据库连接对象
	public Statement stmt = null;	//Statement对象,用于执行SQL语句
	public ResultSet rs = null;		//结果集对象
        
   
    public DBConnection()
    {
    	conn = null;
    }

	public  Connection getConn()
	{

		DBType= new Function().StrToInt(getPara("DBType"));
	
		switch(DBType)
		{
			case 1:return(getConnToMySql());
			default:return null;
		}	
	}
	
	
	public String getPara(String ParaName) 
	{
		FileName="../DBConfig.property";
		Properties prop= new Properties();
		try
		{
			InputStream is=getClass().getResourceAsStream(FileName);
			prop.load(is);
			if(is!=null) is.close();
		}
		catch(Exception e) {
			return "Error!";
		}
		return prop.getProperty(ParaName);
	}
	
	
    public Connection getConnToMySql()
    {
		try{
	 		MySqlDriver = getPara("MySQLDriver");	
	    	MySqlURL = getPara("url");
	    	username = getPara("username");
	    	password = getPara("password");

	    	Class.forName(MySqlDriver).newInstance();
	    	conn = DriverManager.getConnection(MySqlURL,username,password);
	    	}catch(Exception e){
	    		e.printStackTrace();
		    	//return "操作数据库出错,请仔细检查" ;
		    	//System.err.println(e.getMessage());
	    	}
	    return conn;
    }

	/**
	 * 功能:关闭数据库连接
	 */
	public void close() {
		try { // 捕捉异常
			if (rs != null) {
				rs.close();// 关闭结果集对象
			}
			if (stmt != null) {
				stmt.close(); // 关闭Statement对象
			}
			if (conn != null) {
				conn.close(); // 关闭数据库连接对象
			}
		} catch (Exception e) {// 处理异常
			e.printStackTrace(System.err);// 输出异常信息
		}
	}

	/**
	 * 功能:更新数据
	 *
	 * @param sql
	 * @return
	 */
	public int executeUpdate(String sql) {
		int result = 0;// 更新数据的记录条数
		try {// 捕捉异常
			conn = getConnToMySql();// 获取数据库连接
			// 创建用于执行SQL语句的Statement对象
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			result = stmt.executeUpdate(sql);// 执行SQL语句
		} catch (SQLException ex) {// 处理异常
			result = 0;// 指定更新数据的记录条数为0,表示没有更新数据
			ex.printStackTrace();// 输出异常信息
		}
		try {// 捕捉异常
			stmt.close();// 关闭用于执行SQL语句的Statement对象
		} catch (SQLException ex1) {// 处理异常
			ex1.printStackTrace();// 输出异常信息
		}
		return result;// 返回更新数据的记录条数
	}

	public static void main(String[] args) {
		new DBConnection().getConnToMySql();
	}
    
}
最近下载更多
顾北城  LV12 3月28日
yyidaaa  LV1 2023年12月14日
223664015  LV6 2023年6月14日
2063066273  LV2 2023年6月14日
1379585889  LV11 2023年6月12日
996171721  LV7 2023年5月29日
mmmkkl  LV2 2023年5月10日
Honliy  LV4 2023年2月6日
194897762  LV4 2022年12月26日
200171  LV9 2022年11月1日
最近浏览更多
孙纪龙啊  LV10 4月20日
cai647566  LV1 4月16日
lyh1989  LV34 4月8日
顾北城  LV12 3月28日
taoshen95  LV14 3月27日
jc121140  LV3 3月23日
笑得很美  LV11 3月13日
wjh007  LV4 2月29日
heshao  LV2 2月7日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友