sqlresultsetmetadata.java

来自「Java 2实用编程百例,一些比较实用的J2SE小程序,可以通过这些程序熟悉JA」· Java 代码 · 共 120 行

JAVA
120
字号
package ch03.section09;

import java.sql.*;

public class SQLResultSetMetaData
    implements java.sql.ResultSetMetaData {
  private SResultSet tsql;
  public SQLResultSetMetaData(SResultSet result) {
    tsql = result;
  }

  public int getColumnCount() throws SQLException {
    return tsql.numcols();
  }

  public boolean isAutoIncrement(int column) throws SQLException {
    return false;
  }

  public boolean isCaseSensitive(int column) throws SQLException {
    return true;
  }

  public boolean isSearchable(int column) throws SQLException {
    return true;
  }

  public boolean isCurrency(int column) throws SQLException {
    return false;
  }

  public int isNullable(int column) throws SQLException {
    return columnNoNulls;
  }

  public boolean isSigned(int column) throws SQLException {
    return true;
  }

  public int getColumnDisplaySize(int column) throws SQLException {
    SColumn col = tsql.columnAtIndex(column - 1);
    return col.size;

  }

  public String getColumnLabel(int column) throws SQLException {
    SColumn col = tsql.columnAtIndex(column - 1);
    return (col.table + "." + col.name);
  }

  //SColumn的名称
  public String getColumnName(int column) throws SQLException {
    SColumn col = tsql.columnAtIndex(column - 1);
    return col.name;
  }

  public String getSchemaName(int column) throws SQLException {
    return "";
  }

  public int getPrecision(int column) throws SQLException {
    throw new SQLException("tinySQL does not support precision.");
  }

  public int getScale(int column) throws SQLException {
    throw new SQLException("does not support scale.");
  }

  //获取TABLE的名称
  public String getTableName(int column) throws SQLException {
    SColumn col = tsql.columnAtIndex(column - 1);
    return col.table;
  }

  public String getCatalogName(int column) throws SQLException {
    throw new SQLException(" does not support catalogues.");
  }

  // 返回列的类型
  public int getColumnType(int column) throws SQLException {

    SColumn col = tsql.columnAtIndex(column - 1);
    if (col.type.equals("CHAR")) {
      return Types.CHAR;
    }
    if (col.type.equals("NUMERIC")) {
      return Types.INTEGER;
    }
    throw new SQLException("Unknown data type.");
  }

  // 返回列的类型名称
  public String getColumnTypeName(int column) throws SQLException {
    switch (getColumnType(column)) {
      case Types.INTEGER:
        return "INT";
      case Types.CHAR:
        return "CHAR";
      default:
        return "NULL";
    }
  }

  public boolean isReadOnly(int column) throws SQLException {
    return false;
  }

  public boolean isWritable(int column) throws SQLException {
    return true;
  }

  public boolean isDefinitelyWritable(int column) throws SQLException {
    return true;
  }

  public String getColumnClassName(int column) {
    return null;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?