package orz.treeSquirrels.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * * * 项目名称:test_uploadFile * 类名称:BaseDao * 类描述: 数据库公用类 * 创建人:Mu Xiongxiong * 创建时间:2017-10-11 下午7:05:17 * 修改人:Mu Xiongxiong * 修改时间:2017-10-11 下午7:05:17 * 修改备注: * @version * */ public class BaseDao { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; /** * * @Title: getConnection * @Description: 创建连接 * @param @return * @param @throws ClassNotFoundException * @param @throws SQLException 设定文件 * @return Connection 返回类型 * @throws */ 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=uoloadFileDB;User=sa;Password=171268"); } return conn; } /** * * @Title: executeUpdate * @Description:增删改通用方法 * @param @param sql * @param @param prams * @param @return * @param @throws ClassNotFoundException * @param @throws SQLException 设定文件 * @return int 返回类型 * @throws */ 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; } /** * * @Title: executeQurey * @Description: 查询的操作 * @param @param sql * @param @param prams * @param @return * @param @throws ClassNotFoundException * @param @throws SQLException 设定文件 * @return ResultSet 返回类型 * @throws */ 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; } /** * * @Title: closeAll * @Description: 关闭资源 * @param 设定文件 * @return void 返回类型 * @throws */ 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(); } } } }