反射从入门到精通

Posted beibidewomen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射从入门到精通相关的知识,希望对你有一定的参考价值。

1. 通过反射动态生成SQL语句

Customer.java

package com.xuzhiwen.reflect;

public class Customer {
    private String name;
    private String password;
    private String address;
    private String phone;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "name=\'" + name + \'\\\'\' +
                ", password=\'" + password + \'\\\'\' +
                ", address=\'" + address + \'\\\'\' +
                ", phone=\'" + phone + \'\\\'\' +
                ", sex=\'" + sex + \'\\\'\' +
                \'}\';
    }
}

2.数据库结构如下:

3.主要代码如下:

package com.xuzhiwen.reflect;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("xuzhiwen");
        customer.setPassword("123456");
        customer.setPhone("123456789");

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test",
                "root",
                "123456");

        StringBuffer sql = new StringBuffer();
        sql.append("insert into customer(");
        Class<?> clazz = customer.getClass();
        Method method[] = clazz.getDeclaredMethods();
        List<Object> list = new ArrayList<Object>();

        for (Method me : method) {
            String methodName = me.getName();

                if (methodName.startsWith("get")){
                    Object value = me.invoke(customer);
                    if (value != null){
                        list.add(value);
                        methodName = methodName.substring(3).toUpperCase();
                        sql.append(methodName+",");
                    }
                }
        }
        sql.deleteCharAt(sql.length() -1);
        sql.append(")");
        sql.append(" value(");

        for (int i = 0; i < list.size(); i++) {
            sql.append("?,");
        }

        sql.deleteCharAt(sql.length() -1);
        sql.append(")");

        PreparedStatement ps = conn.prepareCall(sql.toString());
        for (int i = 0; i < list.size(); i++) {
            ps.setObject(i+1,list.get(i));
        }

        ps.executeUpdate();

        if(ps != null){
            ps.close();
        }
        if (conn != null){
            conn.close();
        }

    }
}

4.运行结果如下:

 

以上是关于反射从入门到精通的主要内容,如果未能解决你的问题,请参考以下文章

反射技术菜鸟从入门到精通

反射从入门到精通

Atom编辑器入门到精通 Atom使用进阶

反射的理解

Atom编辑器入门到精通 Atom使用进阶

Atom编辑器入门到精通 Atom使用进阶