
Java_反射_SQL增删改查
发布日期:2021-05-07 00:20:26
浏览次数:19
分类:精选文章
本文共 4986 字,大约阅读时间需要 16 分钟。
Java_反射_SQL增删改查
#Java Bean
package com.hp.beans;/** * Java类 User */public class user {private Integer uid;private String uname;private String upass;private String isadmin; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getIsadmin() { return isadmin; } public void setIsadmin(String isadmin) { this.isadmin = isadmin; } @Override public String toString() { return "user{" + "uid=" + uid + ", uname='" + uname + '\'' + ", upass='" + upass + '\'' + ", isadmin='" + isadmin + '\'' + '}'; } public user(Integer uid, String uname, String upass, String isadmin) { this.uid = uid; this.uname = uname; this.upass = upass; this.isadmin = isadmin; } public user() { }}#获得链接
package com.hp.reflect;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** * 获取Sql连接的类 */public class GetConnection{ /** * 获取sql的连接对象 * @return */ public Connection getCon(){ //定义连接对象 Connection conn=null; try { //加载MySql数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //通过DriverManager.getConnection(url,username,password)获取数据库的连接 conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } //将获取的数据库连接返回 return conn; }}#增删改查
package com.hp.reflect;import com.hp.beans.user;import java.lang.reflect.Field;import java.sql.*;import java.util.ArrayList;import java.util.List;/** * 反射_sql查询 */public class DaoImpl { //定义接收的类型为泛型集合 publicList select(T t) throws SQLException, IllegalAccessException, InstantiationException { //Class.getSimpleName()通过泛型获取对象名 String sql="select * from "+t.getClass().getSimpleName(); //实例化List泛型 List li=new ArrayList (); //实例化GetConnection类,通过getCon方法获取Sql的连接 Connection con = new GetConnection().getCon(); //通过连接的prepareStatement()的方法执行sql语句 //调用executeQuery()执行查询获取查询的结果集合 ResultSet res = con.prepareStatement(sql).executeQuery(); //通过resultSet.getMetadata()将结果集合转换为ResultMetaData对象 ResultSetMetaData md = res.getMetaData(); //获取结果的列数 int count = md.getColumnCount(); //获取java对象的属性数组 Field[] filds = t.getClass().getDeclaredFields(); //调用next方法遍历结果集合 while(res.next()) { //将泛型类实例化为对象 T t2 = (T) t.getClass().newInstance(); //循环泛型对象的属性值 for (int i = 0; i < filds.length; i++) { //输出当前的属性 System.out.println(filds[i]); //循环遍历当前结果集中的对象的属性 for (int k = 0; k < count; k++) { //获取此条信息的列的名字 String columnName = md.getColumnName(k + 1); //判断结果集中的列名与Java类中的属性名是否对应 if (filds[i].getName().toUpperCase().equals(columnName.toUpperCase())) { //修改类的Accessible为true,使当前属性可以修改 filds[i].setAccessible(true); //res.getObject(列名) 获取当前这条信息的列名的数据 //调用Filed类的set方法,给当前对象赋值 filds[i].set(t2, res.getObject(columnName)); } } } //将循环赋值完成的类添加到集合中 li.add(t2); } //返回结果 return li; } /** * 更新 * @param t * @param * @return */ public T update(T t){ try { Field[] filds = t.getClass().getDeclaredFields(); filds[0].setAccessible(true); String tiao=""; for(int i=1;i 0){ System.out.println(" successful!"); system.out.println("error!"); catch (illegalaccessexception | sqlexception e) e.printstacktrace(); return null; ** * 添加 @param t * @return * @throws IllegalAccessException */ public T insert(T t) throws IllegalAccessException { Field[] filds = t.getClass().getDeclaredFields(); String columnName=""; String columnValue=""; for (int i=0;i 0){ System.out.println("Successful!"); } else{ System.out.println("Error!"); } } catch (SQLException e) { e.printStackTrace(); } return null; } /** * 反射删除 * @param t * @param * @return */ public T delete(T t) throws SQLException { Field fild = t.getClass().getDeclaredFields()[0]; fild.setAccessible(true); try { String sql="delete from "+t.getClass().getSimpleName()+" where "+fild.getName()+" = "+fild.get(t); System.out.println(sql); int i=new GetConnection().getCon().prepareStatement(sql).executeUpdate(); if(i>0){ System.out.println("Successful!"); } else{ System.out.println("Error!"); } } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }}
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月09日 06时16分48秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
dll详解
2021-05-09
c++ static笔记
2021-05-09
C++中头文件相互包含与前置声明
2021-05-09
JQuery选择器
2021-05-09
MVC中在一个视图中,怎么加载另外一个视图?
2021-05-09
SQL--存储过程
2021-05-09
MVC学习系列5--Layout布局页和RenderSection的使用
2021-05-09
MVC学习系列13--验证系列之Remote Validation
2021-05-09
多线程之volatile关键字
2021-05-09
2.1.4奇偶校验码
2021-05-09
2.2.2原码补码移码的作用
2021-05-09
多线程之Lock显示锁
2021-05-09
ForkJoinPool线程池
2021-05-09
【Struts】配置Struts所需类库详细解析
2021-05-09
Java面试题:Servlet是线程安全的吗?
2021-05-09
DUBBO高级配置:多注册中心配置
2021-05-09
Java集合总结系列2:Collection接口
2021-05-09
Linux学习总结(九)—— CentOS常用软件安装:中文输入法、Chrome
2021-05-09
大白话说Java反射:入门、使用、原理
2021-05-09
集合系列 Set(八):TreeSet
2021-05-09