封装JAVA数据库操作

感谢这位博主分享
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class BaseDao {
         public final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
         public final static String URL = "jdbc:sqlserver://localhost:1433;databaseName=boor";
         public final static String USERNAME = "sa";
         public final static String PASSWORD = "";
 
         **
          *
          * @return 获得数据库连接
          * @throws SQLException
          * @throws ClassNotFoundException
          *
         public Connection getConn() {
                   Connection conn = null;
                   try {
                            Class.forName(DRIVER);
                   } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                   }
                   try {
                            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                   } catch (SQLException e) {
                            e.printStackTrace();
                   }
                   return conn;
         }
 
         **
          * 关闭数据库连接
          * @param conn数据库连接
          * @param prsts  PreparedStatement 对象
          * @param rs结果集
          *
         public void closeAll(Connection conn, PreparedStatement prsts, ResultSet rs) {
                   if (rs != null) {
                            try {
                                     rs.close();
                            } catch (SQLException e) {
                                     e.printStackTrace();
                            }
                   }
                   if (prsts != null) {
                            try {
                                     prsts.close();
                            } catch (SQLException e) {
                                     e.printStackTrace();
                            }
                   }
                   if (conn != null) {
                            try {
                                     conn.close();
                            } catch (SQLException e) {
                                     e.printStackTrace();
                            }
                   }
         }
 
         **
          * 执行增、删、改SQL语句
          *
          * @param sql
          *            sql语句
          * @param param
          *            值集
          * @param type
          *            值类型集
          * @return 受影响的行数
          *
         public int executeUpdate(String sql, Object[] param, int[] type) {
                   int rows = 0;
                   Connection conn = this.getConn();
                   PreparedStatement prsts = null;
                   try {
                            prsts = conn.prepareStatement(sql);
                            for (int i = 1; i <= param.length; i++) {
                                     prsts.setObject(i, param[i - 1], type[i - 1]);
                            }
                            rows = prsts.executeUpdate();
                   } catch (SQLException e) {
                            e.printStackTrace();
                   } finally {
                            this.closeAll(conn, prsts, null);
                   }
                   return rows;
         }
 
         **
          * 执行查询SQL语句
          *
          * @param sql
          *            sql语句
          * @param param
          *            值集
          * @param type
          *            值类型集
          * @return 结果集
          *
         public List executeQuery(String sql, Object[] param, int[] type) {
                   ResultSet rs = null;
                   List list = null;
                   Connection conn = this.getConn();
                   PreparedStatement prsts = null;
                   try {
                            prsts = conn.prepareStatement(sql);
                            for (int i = 1; i <= param.length; i++) {
                                     prsts.setObject(i, param[i - 1], type[i - 1]);
                            }
                            rs = prsts.executeQuery();
                            list = new ArrayList();
                            ResultSetMetaData rsm = rs.getMetaData();
                            Map map = null;
                            while (rs.next()) {
                                     map = new HashMap();
                                     for (int i = 1; i <= rsm.getColumnCount(); i++) {
                                               map.put(rsm.getColumnName(i), rs.getObject(rsm.getColumnName(i)));
                                     }
                                     list.add(map);
                            }
                   } catch (SQLException e) {
                            e.printStackTrace();
                   }finally{
                            this.closeAll(conn, prsts, rs);
                   }
                   return list;
         }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值