关于JDBC的封装
来源:互联网 发布:迷宫的生成算法 编辑:程序博客网 时间:2024/06/06 08:49
package JDBC_Example;import java.sql.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by john on 2016/7/3. * @author zzy */class ConnectionZzy{ //drivername private static final String DRIVERNAME = "com.mysql.jdbc.Driver"; //url private static final String URL = "jdbc:mysql://localhost:3306/jdbc_example"; //username private static final String USERNAME = "root"; //password private static final String USERPASSWORD = ""; private Connection connnection = null; private PreparedStatement preparedStatement = null; private ResultSet resultSet = null; static{ try { Class.forName(DRIVERNAME); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } } private Connection getConnection() { try { connnection = DriverManager.getConnection(URL,USERNAME,USERPASSWORD); } catch (SQLException e) { System.out.println(e.getMessage()); } return connnection; } //select something private ResultSet executeQuery(String sql,Object[] params) { try { connnection = this.getConnection(); preparedStatement = connnection.prepareStatement(sql); if(params!=null){ for(int i=0;i<params.length;i++){ preparedStatement.setObject(i+1,params[i]); } } resultSet=preparedStatement.executeQuery(); } catch (SQLException e) { System.out.println(e.getMessage()); } return resultSet; } //select something return a arraylist List<Object> excuteQuerys(String sql,Object[] params) { List<Object> list = new ArrayList<>(); ResultSetMetaData rsmd; int columnCount; ResultSet rs = executeQuery(sql,params); try { rsmd = rs.getMetaData(); columnCount = rsmd.getColumnCount();//表的列数 while (rs.next()) { Map<String,Object> map=new HashMap<>(); for(int i=1;i<=columnCount;i++){ map.put(rsmd.getColumnLabel(i),rs.getObject(i)); } list.add(map); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { closeAll(); } return list; } //update something,insert something,delete something int executeUpdate(String sql,Object[] params) { int affectedLine = 0; try { connnection = this.getConnection(); preparedStatement = connnection.prepareStatement(sql); if(params!= null) { for(int i=0;i<params.length;i++){ preparedStatement.setObject(i+1,params[i]); } } affectedLine = preparedStatement.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { closeAll(); } return affectedLine; } //colse something private void closeAll() { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { System.out.println(e.getMessage()); } } if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { System.out.println(e.getMessage()); } } if (connnection != null) { try { connnection.close(); } catch (SQLException e) { System.out.println(e.getMessage()); } } }}
0 0
- 关于JDBC的封装
- 关于jdbc-util的封装
- 关于JDBC封装的一些自己的建议
- java 关于封装jdbc操作接口的一些做法
- JDBC中关于封装和读取配置文件properties的问题
- JDBC的封装
- JDBC 的封装
- Jdbc的普通封装
- JDBC的封装类
- JDBC的封装
- JDBC使用方法的封装
- JDBC连接的封装
- JAVA-JDBC的封装
- JDBC的封装
- 简单的jdbc封装+servlet的封装
- 自己的jdbc轻封装
- JDBC的封装类构建
- JDBC 对数据库连接的封装
- 登录验证码 tjl
- 人工智能突破!谷歌AI可以相互交流并加密
- Ubuntu 16.04搭建git服务器
- = =第一篇杂记!
- PowerDesigner V16.5 安装文件 及 破解文件
- 关于JDBC的封装
- servlet request获取form值
- android常用控件
- android 混淆发布jar
- Android 优雅的为RecyclerView添加HeaderView和FooterView
- Mysql从某个字段的每类中取最大最小值
- 科学论文常用语
- Android 艺术开发探索 第一章 Activity 的生命周期和启动模式观后感
- Bootstrap 学习之 (十七)------ 面板