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

本文共 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

你可能感兴趣的文章
CSU-2046: sequence
查看>>
【mongo】可以用localhost启动,无法用ip启动问题的解决
查看>>
【QT】视频播放
查看>>
HTML中使用javascript解除禁止input输入框代码:
查看>>
揭开Redis的神秘面纱
查看>>
Object流
查看>>
网关服务器——个人学习
查看>>
bzoj1293 [SCOI2009]生日礼物
查看>>
转 10 个 Nginx 的安全提示
查看>>
jQuery UI-draggable参数学习
查看>>
Windows Phone开发(8):关于导航的小技巧 转:http://blog.csdn.net/tcjiaan/article/details/7285062...
查看>>
React零碎知识点回顾
查看>>
字符串类型 字符串下标 字符串的方法 切片 for循环的一些总结
查看>>
Redis
查看>>
记一次mysql的preparedStatement使用超限问题
查看>>
Ajax学习笔记1之第一个Ajax应用程序
查看>>
Leetcode: Kth Largest Element in an Array
查看>>
响应式web网站设计制作方法
查看>>
表面stl(栈)
查看>>
382 Linked List Random Node 链表随机节点
查看>>