首页>代码>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  LV11 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日
最近浏览更多
阿时23428  LV15 3月18日
罗清晨  LV11 2月21日
zyx113 1月5日
暂无贡献等级
asddwh  LV12 2023年12月29日
bybingyu  LV2 2023年12月20日
2385649653  LV7 2023年12月13日
hongdongdong  LV12 2023年12月2日
jkjfdgbkl  LV2 2023年11月2日
brownwang  LV1 2023年10月14日
meng2124 2023年10月13日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友