zoukankan      html  css  js  c++  java
  • JDBC 元数据信息 getMetaData()

    数据库元数据信息:

    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    /**
    *
    * 2008-12-7
    *
    * @author <a href="mailto:liyongibm@gmail.com">liyong</a>
    *
    */
    public class DBMD {

        /**
         * @param args
         * @throws SQLException
         * @throws ClassNotFoundException
         */
        public static void main(String[] args) throws SQLException, ClassNotFoundException {

            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/jdbc";
             String user = "root";
            String password = "";
            java.sql.Connection conn = DriverManager.getConnection(url, user, password);
           
            DatabaseMetaData dbmd = conn.getMetaData();//获取数据源的信息,可以用来判断数据源是否支持某些功能,比如事务、批处理等。全在dbmd.support中
            System.out.println("db name: " + dbmd.getDatabaseProductName());
            System.out.println("tx: " + dbmd.supportsTransactions());//判断是否支持事务
            conn.close();
        }

    }

    参数的元数据信息

    public class ParameterMetaTest {

        /**
         * @param args
         * @throws SQLException
         */
        public static void main(String[] args) throws SQLException {
            Object[] params = new Object[] { "lisi", 100f };
            read("select * from user where name=? and  money > ?", params);
        }

        static void read(String sql, Object[] params) throws SQLException {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtils.getConnection();
                ps = conn.prepareStatement(sql);
    //            ParameterMetaData pmd = ps.getParameterMetaData();
    //           int count = pmd.getParameterCount();//可以获取参数个数
                for (int i = 1; i <= params.length; i++) {
                    // System.out.print(pmd.getParameterClassName(i) + " ");  类名
                    // System.out.print(pmd.getParameterType(i) + " ");  类型名
                    // System.out.println(pmd.getParameterTypeName(i));  在数据库中的类型名  

                    //以上这三条不太实用,因为有的数据库引擎是不支持的。
                    ps.setObject(i, params[i - 1]);
                }

                rs = ps.executeQuery();

                while (rs.next()) {
                    System.out.println(rs.getInt("id") + " "
                            + rs.getString("name") + " " + rs.getDate("birthday")
                            + " " + rs.getFloat("money"));
                }

            } finally {
                JdbcUtils.free(rs, ps, conn);
            }
        }

    }

    结果集的元数据信息

    public class ResultSetMetaDataTest {

        /**
         * @param args
         * @throws SQLException
         */
        public static void main(String[] args) throws SQLException {
            List<Map<String, Object>> datas = read("select id, name as n from user where id < 5");    //n是别名 name是表中列本来的的名字
            System.out.println(datas);
        }

        static List<Map<String, Object>> read(String sql) throws SQLException {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtils.getConnection();
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                ResultSetMetaData rsmd = rs.getMetaData();
                int count = rsmd.getColumnCount();//获取列数
                String[] colNames = new String[count];
                for (int i = 1; i <= count; i++) {
                    // System.out.print(rsmd.getColumnClassName(i) + " "); //获取数据类型
                    // System.out.print(rsmd.getColumnName(i) + " "); //获取列本来的名字
                    // System.out.println(rsmd.getColumnLabel(i));
                    colNames[i - 1] = rsmd.getColumnLabel(i);//获取列的别名,如果没有别名就是列的本名     
                }
                List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();

                while (rs.next()) {
                    Map<String, Object> data = new HashMap<String, Object>();
                    for (int i = 0; i < colNames.length; i++) {
                        data.put(colNames[i], rs.getObject(colNames[i]));
                    }
                    datas.add(data);
                }
                return datas;
            } finally {
                JdbcUtils.free(rs, ps, conn);
            }
        }

    }

  • 相关阅读:
    Linux perf命令详解及常用参数解析
    pidstat 命令(Linux 进程使用资源情况采样)
    Katalon Studio入门学习之三种获取元素方式
    用Spring和c3p0工具简单的实现增删改查
    Spring AOP(面向切面示例)
    Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
    PowerDesigner最基础的使用方法入门学习(转)
    Struts文件上传
    struts转换器
    Struts拦截器
  • 原文地址:https://www.cnblogs.com/flying607/p/3462661.html
Copyright © 2011-2022 走看看