zoukankan      html  css  js  c++  java
  • jdbc不写bean类还能返回查询的结果

    ResultSetMetaData metaData = rs.getMetaData();

    jdbc中有一步是返回结果集,在结果集中有个getMetaData()的方法就可以得到数据库中的所有值,例如字段名、行数、列数等等可以用debug看看

    以下是jdbc的增删改   和   查的封装类,不用写bean类就可直接使用

    package com.hp.factory;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class AllFactory {
        final static String url = "jdbc:mysql://localhost:3306/project01?unicode=true&characterEncoding=UTF-8";
        final static String user = "root";
        final static String password = "root";
        final static String driver = "com.mysql.jdbc.Driver";
        public static Connection getConn()throws Exception {
            Class.forName(driver);
            return DriverManager.getConnection(url, user, password);
        }
        public static List<Map<String, Object>> executeQuery(String sql)throws Exception {
            List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
            Connection conn = getConn();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ResultSetMetaData metaData = rs.getMetaData();
                Map<String, Object> map = new HashMap<String, Object>();
                for(int i=1;i<=metaData.getColumnCount();i++) {
                    map.put(metaData.getColumnLabel(i), rs.getObject(i));
                }
                list.add(map);
            }
            rs.close();
            ps.close();
            conn.close();
            return list;
        }
        public static int excuteUpdate(String sql) throws Exception {
            Connection conn = getConn();
            PreparedStatement ps = conn.prepareStatement(sql);
            int i = ps.executeUpdate();
            ps.close();
            conn.close();
            return i;
        }

    }

  • 相关阅读:
    react模拟后端接口及提交方式 json-server
    CSS基础知识(概念、块级元素、行内元素、选择器)
    HTML基础知识(表格、表单)
    HTML基础知识(常见元素、列表、链接元素、图片元素)
    【Ionic】---AngularJS扩展基本布局
    多行文本溢出显示省略号…
    【AngularJs】---"Error: [ng:areq] Argument 'fn' is not a function, got undefined"
    【AngularJs】---$sce 输出Html
    【AngularJs】---实现select的ng-options
    浅谈WLAN干扰与抗干扰技术
  • 原文地址:https://www.cnblogs.com/lihui123/p/13842863.html
Copyright © 2011-2022 走看看