⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.java

📁 JAVA数据库编程实例随书源码 JAVA数据库编程实例随书源码
💻 JAVA
字号:
package sqlserver;

import java.sql.*;

/**
 * Title:        Server数据库访问和操纵
 * Description:  Java语言演示程序:Server数据库访问和操纵,用于北京师范大学计算机系Java课程教学示范。
 * Copyright:    Copyright (c) 2002
 * Company:      北京师范大学计算机系
 * @author 孙一林
 * @version 1.0
 */

class Display extends Object {        // 定义转换Java类型的显示
    ResultSet theResultSet;
    String theResult;
    public void display(){}
    public void setResult(ResultSet result){
        theResultSet= result;
    }
    public String getString() throws SQLException,NoClassDefFoundError {
        theResult= "";
        ResultSetMetaData metaData= theResultSet.getMetaData();
        int colcount = metaData.getColumnCount();
        int colSize[]= new int[colcount];
        String colLabel[]= new String[colcount];
        int colType[]= new int[colcount];
        String colTName[]= new String[colcount];
        int colPrec[]= new int[colcount];
        int colScale[]= new int[colcount];
        theResult +="\n";
        for(int i= 1;i<= colcount;i++) {
            colSize[i-1] = metaData.getColumnDisplaySize(i);
            colLabel[i-1]= metaData.getColumnLabel(i);
            colType[i-1] = metaData.getColumnType(i);
            colTName[i-1]= metaData.getColumnTypeName(i);
            colPrec[i-1] = metaData.getPrecision(i);
            colScale[i-1]= metaData.getScale(i);
            if(colSize[i-1]<1+ colLabel[i-1].length())
                colSize[i-1]= 1+colLabel[i-1].length();
            theResult+= rightPad(colLabel[i-1],colSize[i-1]);
        }
        theResult +="\n\n";
        int row= 0;
        while(theResultSet.next()) {
            row++;
            for(int i=1;i<= colcount;i++){
                String colvalue= theResultSet.getString(i);
                if(colvalue== null)
                    colvalue="";
                theResult+= rightPad(colvalue,colSize[i-1]);
            }
            theResult+="\n";
        }
        theResult+="\n(" +row+ "rows included)\n";
        return theResult;
    }
    private String rightPad(String s,int len){
        int curlen= s.length();
        if(curlen>len)
            return repString("*",len);
        return (s+repString(" ",(len-curlen)));
    }
    private String repString(String s,int times){
        String result="";
        for(int i=0;i<times;i++)
            result+= s;
        return result;
    }
}

⌨️ 快捷键说明

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