当前位置:网站首页>jdbc接口文档参考,jdbc接口方法逻辑探究

jdbc接口文档参考,jdbc接口方法逻辑探究

2022-08-11 05:28:00 秋天已北

sqlsever jdbc 接口文档

jdbc 接口测试


package com.lqz.testjava;

import java.sql.*;

public class TablePrintStructure {
    


    public static void main(String[] args) throws Exception {
    
// String driver = "com.mysql.cj.jdbc.Driver";
        String driver = "org.postgresql.Driver";
// String url = "jdbc:mysql://localhost:3306/book_sc?serverTimezone=GMT";
        String url = "jdbc:postgresql://192.168.0.130:5432/test_db";
        String user = "user_name";
        String password = "pass_word";

        printTableStructure(driver, url, user, password);
    }

    /** * 打印所有表结构 * * @param driver driver * @param url url * @param user user * @param password password * @throws Exception exception */
    private static void printTableStructure(String driver, String url, String user, String password) throws Exception {
    
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
        DatabaseMetaData metaData = connection.getMetaData();
        System.out.println(metaData);
        // 获取所有表
        ResultSet tableResultSet = metaData.getTables(
                null,
                null,
                "test%",
                new String[]{
    "TABLE"});
        System.out.println(tableResultSet.toString());

        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("select * from test_tab");
        System.out.println("---" + rs.getClass());


        while (tableResultSet.next()) {
    
            String tableName = tableResultSet.getString("TABLE_NAME");
            System.out.println("table:" + tableName);

            // 获取表字段结构
            ResultSet columnResultSet = metaData.getColumns(null, "%", tableName, "%");
            while (columnResultSet.next()) {
    
                // 字段名称
                String columnName = columnResultSet.getString("COLUMN_NAME");
                // 数据类型
                String columnType = columnResultSet.getString("TYPE_NAME");
                // 字段长度
                int datasize = columnResultSet.getInt("COLUMN_SIZE");
                // 小数部分位数
                int digits = columnResultSet.getInt("DECIMAL_DIGITS");
                // 是否可为空 1代表可空 0代表不可为空
                int nullable = columnResultSet.getInt("NULLABLE");
                // 描述
                String remarks = columnResultSet.getString("REMARKS");
                System.out.println(columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks);
            }
            System.out.println("=================================");
        }
    }

}


参考微软的接口文档可以学习
如下方法可以找到接口组装的语法内容

idea断点调试
手动执行sql比对

详细sql


SELECT NULL              AS TABLE_CAT,
       NULL              AS TABLE_SCHEM,
       c.relname         AS TABLE_NAME,
       CASE c.relname ~ '^pg_'
           WHEN true THEN CASE c.relname ~ '^pg_toast_'
                              WHEN true THEN CASE c.relkind
                                                 WHEN 'r' THEN 'SYSTEM TOAST TABLE'
                                                 WHEN 'i' THEN 'SYSTEM TOAST INDEX'
                                                 ELSE NULL END
                              WHEN false THEN CASE c.relname ~ '^pg_temp_'
                                                  WHEN true THEN CASE c.relkind
                                                                     WHEN 'r' THEN 'TEMPORARY TABLE'
                                                                     WHEN 'i' THEN 'TEMPORARY INDEX'
                                                                     ELSE NULL END
                                                  WHEN false THEN CASE c.relkind
                                                                      WHEN 'r' THEN 'SYSTEM TABLE'
                                                                      WHEN 'v' THEN 'SYSTEM VIEW'
                                                                      WHEN 'i' THEN 'SYSTEM INDEX'
                                                                      ELSE NULL END
                                                  ELSE NULL END
                              ELSE NULL END
           WHEN false THEN CASE c.relkind
                               WHEN 'r' THEN 'TABLE'
                               WHEN 'i' THEN 'INDEX'
                               WHEN 'S' THEN 'SEQUENCE'
                               WHEN 'v' THEN 'VIEW'
                               ELSE NULL END
           ELSE NULL END AS TABLE_TYPE,
       NULL              AS REMARKS
FROM pg_class c
WHERE true
  AND c.relname LIKE 'test%'
  AND (false OR (c.relkind = 'r' AND c.relname !~ '^pg_'))
ORDER BY TABLE_TYPE, TABLE_NAME

自定义sql语句

        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("select * from test_tab");

返回结果可以参考 SQLServerResultSet 关键字结果
SQLServerResultSet
返回结果引用信息方法及变量名查找
在这里插入图片描述

原网站

版权声明
本文为[秋天已北]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yizhibanjie_1/article/details/125124984