• 新闻发布项目——接口类(BaseDao)


    package bdqn.newsMange.Dao;
    /**
     * 公共类
     * @author Administrator
     *
     */
    import java.sql.*;
    import java.util.List;
    public class BaseDao {
    	Connection conn=null;
    	PreparedStatement ps=null;
    	ResultSet rs=null;
    	
    	public Connection getConnection() throws ClassNotFoundException, SQLException{
    		
    			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    		if(conn==null){
    			conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databasename=newsDB;User=sa;Password=171268");
    		
    		}
    		return conn;
    	}
    	
    	//增删改
    	public int executeUpdate(String sql, List<Object> prams)
    			throws ClassNotFoundException, SQLException {
    		int rel = -1;
    
    		conn = getConnection();
    		/*if(conn.isClosed())
    		{
    			conn=null;
    			conn = getConnection();
    		}*/
    		ps = conn.prepareStatement(sql);
    		
    		if (prams != null) {
    			for (int i = 0; i < prams.size(); i++) {
    				ps.setObject(i + 1, prams.get(i));
    			}
    		}
    
    		rel = ps.executeUpdate();
    		return rel;
    	}
    	
    	//查询
    	public ResultSet executeQurey(String sql,List<Object>prams) throws ClassNotFoundException, SQLException{
    		conn=getConnection();
    		ps=conn.prepareStatement(sql);
    		if(prams!=null){
    			for (int i = 0; i < prams.size(); i++) {
    				ps.setObject(i+1, prams.get(i));
    			}
    		}
    		rs=ps.executeQuery();
    		return rs;
    	}
    	
    	//关闭资源
    	
    	public void closeAll(){
    		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();
    				conn=null;
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

  • 相关阅读:
    JSOIWC2019游记
    基础网络流题单
    【题解】Luogu P2472 [SCOI2007]蜥蜴
    【题解】Luogu P2057 [SHOI2007]善意的投票
    凸包略解
    【题解】Luogu P4324 [JSOI2016]扭动的回文串
    【题解】Luogu P4054 [JSOI2009]计数问题
    kruscal重构树略解
    【题解】bzoj 4478 [Jsoi2013]侦探jyy
    【题解】4465 [Jsoi2013]游戏中的学问
  • 原文地址:https://www.cnblogs.com/a1111/p/6540329.html
Copyright © 2020-2023  润新知