java-BaseDao

博客围绕Java的BaseDao展开,但具体内容缺失。BaseDao在Java后端开发中是重要的数据访问对象,可封装通用数据库操作,提高代码复用性和开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package dao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    
    public class BaseDao {
    	private static String driver;
    	private static String url;
    	private static String user;
    	private static String pwd;
    	protected Connection conn;
    	private static void init(){
    		Properties properties=new Properties();
    		InputStream is=BaseDao.class.getClassLoader().getResourceAsStream("basedao.properties");
    		try {
    			properties.load(is);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		driver=properties.getProperty("driver");
    		url=properties.getProperty("url");
    		user=properties.getProperty("user");
    		pwd=properties.getProperty("pwd");
    		
    		try {
    			Class.forName(driver);
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	static{
    		init();
    	}
    	
    	protected Connection getConnection(){
    		try {
    			if (conn==null||conn.isClosed()) {
    				conn=DriverManager.getConnection(url,user,pwd);
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return conn;
    	}
    
    	protected void closeAll(Connection conn,PreparedStatement ps,ResultSet rs) {
    		if (rs!=null) {
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    		}
    		if (ps!=null) {
    			try {
    				ps.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		if (conn!=null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	protected int executeUpdate(String sql,Object... param) {
    		int result=0;
    		conn=getConnection();
    		PreparedStatement ps=null;
    		try {
    			ps=conn.prepareStatement(sql);
    			for (int i = 0; i < param.length; i++) {
    				ps.setObject(i+1, param[i]);
    			}
    			result=ps.executeUpdate();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			closeAll(conn, ps, null);
    		}
    		return result;
    	}
    	
    	protected ResultSet executeQuery(String sql,Object... param) {
    		ResultSet rs=null;
    		conn=getConnection();
    		PreparedStatement ps=null;
    		try {
    			ps=conn.prepareStatement(sql);
    			for (int i = 0; i < param.length; i++) {
    				ps.setObject(i+1, param[i]);
    			}
    			rs=ps.executeQuery();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return rs;
    	}
    	
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值