• 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);
            }
        }

    }

  • 相关阅读:
    requestWindowFeature()的应用(转载)
    Android美工坊selector选择器的使用(转载)
    Activity强制横屏竖屏
    ActivityGroup的简单用法(2)Demo展示讲解
    AndroidGUI25:定制Activity的标题栏(Titlebar)(转载)
    android背景选择器selector用法汇总
    eclipse的注释字体大小如何修改?不改变代码的字体
    QML基础——QML国际化(中文示例)
    QML基础——在C++程序中使用QML
    App for Android (4) —— Eclipse篇
  • 原文地址:https://www.cnblogs.com/flying607/p/3462661.html
Copyright © 2020-2023  润新知