package bdqn.studentSys.Dao; /** * 数据库帮助类 * @author Administrator * */ import java.sql.*; public class BaseDao { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; //连接数据库 public void getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databasename=mydb;User=sa;Password=171268"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //增删改 public int executeUpdate(String sql,Object[]prams) throws SQLException{ int rel=0; getConnection(); ps=conn.prepareStatement(sql); if(prams!=null){ for (int i = 0; i < prams.length; i++) { ps.setObject(i+1, prams[i]); } } rel=ps.executeUpdate(); return rel; } //查询 public ResultSet executeQurey(String sql,Object[]prams) throws SQLException{ getConnection(); ps=conn.prepareStatement(sql); if(prams!=null){ for (int i = 0; i < prams.length; i++) { ps.setObject(i+1, prams[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(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }