首页>代码>java servlet开发优乐购电子商城完整项目源码(完整前后台功能)>/优乐购商城2.1/Webmasters/src/cn/xwkj/dao/BaseDao.java
package cn.xwkj.dao;
/**
 * ������ݿ���
 * @author 84661
 *
 */
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Properties;

/**
 * BaseDao������ݿ�
 * @author 84661
 *
 */
public class BaseDao {
	//��
	private static String driver;
	//��ַ
	private static String url;
	//�û���
	private static String user;
	//����
	private static String pwd;
	//������ݿ�
	Connection conn = null;
	//ִ��SQL���
	PreparedStatement pstmt = null;
	//���ص�ֵ
	ResultSet rs = null;
	//��ȡ�����ļ�
	static{
		Properties pt = new Properties();
		//�ļ���λ��
		String path ="database.properties";
		//�ļ����Ķ�ȡ
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(path);
		//����
		try {
			pt.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//��ʼ��ȡ
		driver=pt.getProperty("driver");
		url=pt.getProperty("url");
		user=pt.getProperty("user");
		pwd=pt.getProperty("pwd");
		
	}

	/**
	 * ��ݿ�����
	 * @return
	 */
	public Connection getConnection(){
		
		try {
			//������
			Class.forName(driver);
			return DriverManager.getConnection(url,user,pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
		
	}
	/**
	 * �ر���ݿ�
	 */
	public void closeAll(){
		//�رն�ȡ
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//�ر�����
		if (pstmt!=null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//�ر���ݿ�
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * ��ѯ
	 * @param sql
	 * @param param
	 * @return
	 */
	public ResultSet executeQuery(String sql,Object...param){
		
		try {
			
			conn=getConnection();
			pstmt=conn.prepareStatement(sql);
			if (param!=null && param.length>0) {
				for (int i = 0; i < param.length; i++) {
					pstmt.setObject(i+1,param[i]);
				}
			}
			
			return pstmt.executeQuery();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * ��ӣ�ɾ���޸�
	 * @param sql
	 * @param param
	 * @return
	 */
	public int executeUpdate(String sql,Object...param){
		
		try {
			//��ȡ����
			conn= getConnection();
			//��ȡִ�ж���
			pstmt = conn.prepareStatement(sql);
			//�������ֵ
			if (param!=null && param.length>0) {
				for (int i = 0; i < param.length; i++) {
					pstmt.setObject(i+1, param[i]);
				}
			}
			//����ֵ
			return pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll();
		}
		return -1;
		
	}
}

最近下载更多
zhangxinqing1  LV6 2022年10月6日
17693282606  LV12 2022年7月12日
fangen0005  LV25 2022年5月30日
天马行空  LV3 2022年2月7日
dy123123  LV8 2022年1月2日
Tiffany & Co.  LV5 2021年12月20日
mwk545924364  LV4 2021年12月9日
凯文  LV5 2021年12月6日
lwp011  LV27 2021年11月5日
maozi0802  LV4 2021年8月6日
最近浏览更多
Daima000  LV4 2024年12月3日
15719908287  LV9 2024年6月19日
微信网友_6790966844739584  LV5 2024年6月10日
郭宇航裹裹  LV5 2024年6月7日
阿时23428  LV15 2024年3月18日
罗清晨  LV13 2024年2月21日
zyx113 2024年1月5日
暂无贡献等级
asddwh  LV13 2023年12月29日
bybingyu  LV2 2023年12月20日
2385649653  LV7 2023年12月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友