博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
针对MySql封装的JDBC通用框架类(包含增删改查、JavaBean反射原理)
阅读量:5788 次
发布时间:2019-06-18

本文共 5954 字,大约阅读时间需要 19 分钟。

 

 

package com.DBUtils;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.bean.UserInfo;public class JdbcUtils {    private static final String USERNAME = "system";//数据库用户名    private static final String PASSWORD = "zZ123456";//数据库密码    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";//驱动信息    private static final String URL = "jdbc:oracle:thin:@localhost:1521:bookstore";//数据库地址    private Connection connection = null;    private PreparedStatement statement = null;    private ResultSet resultSet = null;    public JdbcUtils() {        try {            Class.forName(DRIVER);            System.out.println("Connect database Successlly!");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }        /**     * 得到数据库连接     * @return connection     */    public Connection getConnection() {        try {            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);        } catch (SQLException e) {            e.printStackTrace();        }        return connection;    }        /**     * 更新操作(增删改)     * @param sql     * @param params     * @return boolean     * @throws SQLException     */    public boolean updateByPreparedStatement(String sql,List params) throws SQLException {        boolean flag = false;        int result = -1;        statement = connection.prepareStatement(sql);        int index = 1;        if(params != null && !params.isEmpty()){            for(int i=0; i
0 ? true : false; } /** * 查询单条数据 * @param sql * @param params * @return * @throws SQLException */ public Map
findSimpleResult(String sql,List
params) throws SQLException { Map
map = new HashMap
(); statement = connection.prepareStatement(sql); int index = 1; if(params != null && !params.isEmpty()){ for(int i=0; i
> findModeResult(String sql, List
params) throws SQLException{ List
> list = new ArrayList
>(); int index = 1; statement = connection.prepareStatement(sql); if(params != null && !params.isEmpty()){ for(int i = 0; i
map = new HashMap
(); for(int i=0; i
T findSimpleRefResult(String sql, List
params, Class
cls )throws Exception{ T resultObject = null; int index = 1; statement = connection.prepareStatement(sql); if(params != null && !params.isEmpty()){ for(int i = 0; i
List
findMoreRefResult(String sql, List
params, Class
cls )throws Exception { List
list = new ArrayList
(); int index = 1; statement = connection.prepareStatement(sql); if(params != null && !params.isEmpty()){ for(int i = 0; i
params = new ArrayList
(); params.add("02"); params.add("02"); try { boolean flag = jdbcUtils.updateByPreparedStatement(sql, params); System.out.println(flag); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ jdbcUtils.Conn(); } */ /*******************删*********************/ //删除名字为张三的记录 /* String sql = "delete from userinfo where username = ?"; List params = new ArrayList(); params.add("7"); try { boolean flag = jdbcUtils.updateByPreparedStatement(sql, params); System.out.println(flag); } catch (SQLException e) { e.printStackTrace(); } finally{ jdbcUtils.Conn(); } */ /*******************改*********************/ /* String sql = "update userinfo set password = ? where username = ? "; List params = new ArrayList(); params.add("233333"); params.add("01"); boolean flag; try { flag = jdbcUtils.updateByPreparedStatement(sql, params); System.out.println(flag); } catch (SQLException e) { e.printStackTrace(); } finally{ jdbcUtils.Conn(); } */ /*******************查*********************/ //不利用反射查询多个记录 /* String sql2 = "select * from userinfo"; List
> list; try { list = jdbcUtils.findModeResult(sql2, null); System.out.println(list); //list toSteing() 直接输出 } catch (SQLException e) { e.printStackTrace(); }finally{ jdbcUtils.Conn(); }*/ /* //不利用反射查询单个记录 (出不来!!!) String sql2 = "select * from userinfo where username = ?"; Map
map;// List
params = new ArrayList(); params.add("01"); Map
userinfo ; try{ userinfo = jdbcUtils.findSimpleResult(sql2, params); System.out.println(userinfo); }catch(Exception e){ }finally{ jdbcUtils.Conn(); } */ /* //利用反射查询单条记录 String sql = "select * from userinfo where username = ? "; List
params = new ArrayList(); params.add("01"); UserInfo userInfo; try { userInfo = jdbcUtils.findSimpleRefResult(sql, params, UserInfo.class); System.out.print(userInfo); } catch (Exception e) { e.printStackTrace(); }finally{ jdbcUtils.Conn(); } */ /* //利用反射查询多条记录 String sql = "select * from userinfo where username = ?"; List params = new ArrayList(); params.add("02"); List
list; try { list = jdbcUtils.findMoreRefResult(sql, params, UserInfo.class); System.out.print(list); } catch (Exception e) { e.printStackTrace(); }finally{ jdbcUtils.Conn(); } */ }}

 

package com.bean;import java.io.Serializable;import java.util.Arrays;import java.util.Date;public class UserInfo implements Serializable{         private static final long serialVersionUID = 1L;    private String username;    private String password;        public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    @Override    public String toString() {        return "UserInfo [username=" + username + ", password=" + password                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()                + ", toString()=" + super.toString() + "]";    }    }

 

转载于:https://www.cnblogs.com/Roni-i/p/9917302.html

你可能感兴趣的文章
【348天】每日项目总结系列086(2018.01.19)
查看>>
【294天】我爱刷题系列053(2017.11.26)
查看>>
可替换元素和非可替换元素
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>
搭建vsftpd服务器,使用匿名账户登入
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
Java虚拟机管理的内存运行时数据区域解释
查看>>
人人都会深度学习之Tensorflow基础快速入门
查看>>
ChPlayer播放器的使用
查看>>
js 经过修改改良的全浏览器支持的软键盘,随机排列
查看>>
Mysql读写分离
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
Web日志安全分析工具 v2.0发布
查看>>
JS重载
查看>>
python2和python3同安装在Windows上,切换问题
查看>>
php加速工具xcache的安装与使用(基于LNMP环境)
查看>>
android超链接
查看>>
统计数据库大小
查看>>