码迷,mamicode.com
首页 > 其他好文 > 详细

反射从入门到精通(二)

时间:2017-10-05 00:22:14      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:date()   lis   tcl   localhost   .exe   red   call   语句   .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.运行结果如下:

技术分享

 

反射从入门到精通(二)

标签:date()   lis   tcl   localhost   .exe   red   call   语句   .com   

原文地址:http://www.cnblogs.com/beibidewomen/p/7628056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!