• List<bean>转换Jsonarray知识心得


    使用基本的jdbc连接mysql,获取数据。

    创建实体类user:

    package com.json;
    
    public class user {
        private int id;
    private String username;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    private String password;
    private score score;
    
    public user() {
        super();
    }
    public user(String username, String password, com.json.score score) {
        super();
        this.username = username;
        this.password = password;
        this.score = score;
    }
    public score getScore() {
        return score;
    }
    public void setScore(score score) {
        this.score = score;
    }
    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;
    }
    
    }

    创建utildao工具类:

    package com.json;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class utildao {
        public static Connection getConnection(){
            Connection conn=null;
            String url="jdbc:mysql://localhost:3306/test";
            try {
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection(url,"用户名","密码");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("数据库驱动加载出错");
            }catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库出错");
            }
            return conn;
        }
        public static void close(ResultSet rs,PreparedStatement p,Connection conn){
            try {
                rs.close();
                p.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库关闭出错");
            }
        }
        public static void main(String[] args) {
            System.out.println(utildao.getConnection());
        }
    }

    创建查询:

    package com.json;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class jsonjiexi {
        public static ResultSet getjson(){
            Connection conn=null;
            PreparedStatement p=null;
            ResultSet rs=null;
            try {
            conn=utildao.getConnection();
            String sql="select * from userone";
            p=conn.prepareStatement(sql);
            rs=p.executeQuery();
            while(rs.next()){
                //utildao.close(rs, p, conn);
                //System.out.println("查询结束,关闭");
                return rs;
            }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }
        
    }

    最后main方法类:

    package com.json;
    
    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 net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    public class huoqujson {
        public static void main(String[] args) throws SQLException {
            //注释掉此段为,将jdbc获取的resultset转换为list集合
            /*List list=new ArrayList();
            ResultSet rs=jsonjiexi.getjson();
            ResultSetMetaData md=rs.getMetaData();
            int num=md.getColumnCount();
            while(rs.next()){
                Map rowData=new HashMap();
                for(int i=1;i<=num;i++){
                    rowData.put(md.getColumnName(i),rs.getObject(i));
                }
                list.add(rowData);
            }
            System.out.println(list.toString());*/
            //这段实现了将jdbc获取的数据,赋值给实体类,添加进List,最后转换为jsonarray
            user us=new user();
            List<user> list=new ArrayList<user>();
            ResultSet rs=jsonjiexi.getjson();
            while(rs.next()){
                us.setId(rs.getInt("id"));
                us.setUsername(rs.getString("name"));
                us.setPassword(rs.getString("pwd"));
                System.out.println(us.getId()+":"+us.getUsername()+":"+us.getPassword());
            }
            list.add(us);
            JSONArray array=JSONArray.fromObject(list);
            System.out.println(array.toString());
            }
            
        }

    编写期间遇到的错误,在Main方法执行之后,由于没有导入某些jar包报的错误:

    commons-lang-2.4.jar不加这个包 
    java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException

    commons-logging-1.1.1.jar不加这个包 
    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 

    commons-beanutils-1.8.0.jar不加这个包 

    java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
    commons-collections.jar 不加这个包
    java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
    ezmorph-1.0.4.jar不加这个包
    java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
    json-lib-2.3-jdk15.jar不加这个包
    java.lang.NoClassDefFoundError: net/sf/json/JSONObject 

    JAVA修炼塔,技术世界的探知与交流,欢迎你的加入-----群号:535296702
  • 相关阅读:
    C# 通过Attribute制作的一个消息拦截器
    Newtonsoft.Json高级用法
    这些年,我收集的JavaScript代码(一)
    Storm整体架构分析
    Worker的内部工作原理
    Storm源码分析
    Storm集群部署
    Storm 官方文档翻译 --- 消息的可靠性保障
    [转] 如何快速掌握一门新技术/语言/框架
    小狗钱钱读书笔记
  • 原文地址:https://www.cnblogs.com/emperorking/p/6429510.html
Copyright © 2020-2023  润新知