新闻发布项目——接口类(BaseDao)
生活随笔
收集整理的這篇文章主要介紹了
新闻发布项目——接口类(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 blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn!=null){try {conn.close();conn=null;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
}
總結
以上是生活随笔為你收集整理的新闻发布项目——接口类(BaseDao)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 热带低压是什么意思 热带低压解释
- 下一篇: 2017最新顺口溜出炉(超级经典)!