请帮助我解决矢量问题 [英] please help me in a vector problem

查看:68
本文介绍了请帮助我解决矢量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableDatabase extends JFrame{
    public JTableDatabase() {
        Vector columnNames = new Vector();
        Vector data = new Vector();
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connect =DriverManager.getConnection("jdbc:odbc:employeedsn");
            
            String sql = "Select * from emp";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement( md.getColumnName(i) );
            }
            //my Problem start here
            while (rs.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement( rs.getObject(i) );
                }
                data.addElement( row );
            }
            //and ends here
            rs.close();
            stmt.close();
        }
        catch(Exception e) {
            System.out.println( e );
        }
        JTable table = new JTable(data, columnNames);
        
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
        
        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

    public static void main(String[] args) {
    
        JTableDatabase frame = new JTableDatabase();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setTitle("Information Table");
        frame.setVisible(true);
        }
}





我只是想知道循环中发生了什么。

请帮我解决我的问题

提前使用Thanx



I Just want to know what is going in while loop.
Please help me in solving my problem
Thanx in advance

推荐答案

这很简单,

1 - Vector类的参数(这里是)是向量的初始容量和

,根据Oracle文档的构造一个具有指定初始容量的空向量,并且它的容量增量等于零。

因此,向量的容量必须等于列的数量(将向量想象为数据库表中的一行)。

2- Resultset是(!)指向数据库表中的当前行, getobject()方法获取当前指定列的值(此处为 i )此ResultSet对象( rs )的一行作为对象。
it's simple ,
1- the argument of Vector class (here is columns) is initial capacity of your vector and
according to Oracle docs its "Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero."
So the capacity of your vector must be equal to number of your columns (imagine the vector as a row in database table).
2- Resultset is something(!) that points to current row in database table and getobject() method gets the value of the designated column (here is i) in the current row of this ResultSet object (rs) as an Object .


这篇关于请帮助我解决矢量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆