0

我正在尝试以JTable.

这是我ActionListener的主要课程的一部分...

if(e.getSource()==jjb)
{
    String j[] = null;
    Vector r = new Vector();
    Vector finl = new Vector();
    try
    {
        d10.connection();
        int b = d10.getclmncnt();
        for(int g = 1; g<=b+1;g++)
        {
            j = new String[g];

        }
        for(int g = 1; g<=b;g++)
        {
            j[g] = d10.gettableclmn(g);
        }
        Vector v = new Vector();
        for(int g = 1; g<=b;g++)
        {
            v.addElement(j[g]);
        }
        int a = d10.getrwcnt();
        System.out.println("no of rows are   ::::  "+a);

        r = d10.getalldata(b);

        JTable t = new JTable(r,v);
        t.setLayout(null);
        TableColumn tc;
        for(int hh = 0;hh<t.getColumnCount();hh++)
        {               
            tc = t.getColumnModel().getColumn(hh);
            tc.setWidth(75);
        }
        JScrollPane js = new JScrollPane(t);

        js.setBounds(10, 100, 1000, 500);
        p5.add(js);
        p5.repaint();
    }
    catch (ClassNotFoundException e1)
    {
        e1.printStackTrace();
    }
    catch (SQLException e1)
    {
        e1.printStackTrace();
    }
}

这是我与数据库连接的课程...

public void connection()throws SQLException, ClassNotFoundException
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Driver Loaded");
    Properties p = new Properties();
    p.setProperty("user","system");
    p.setProperty("password", "oracle10");
    p.setProperty("url","jdbc:oracle:thin:@Suneel-PC:1521:XE");
    c = DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"),p.getProperty("password"));
    System.out.println("connected");
}

public String gettableclmn(int b2)
{
    String s = null;
    int fh = b2;
    try
    {
        Statement ss = c.createStatement();
        ResultSet rs = ss.executeQuery("select * from data");
        ResultSetMetaData rsmd = rs.getMetaData();
        s = rsmd.getColumnName(fh);
        System.out.println("columns names are     "+rsmd.getColumnName(fh));        
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return s;
}

public int getclmncnt()
{
    int h = 0;

    ResultSetMetaData rsmd;
    try {
        Statement ss = c.createStatement();
        ResultSet rs = ss.executeQuery("select * from data");
        rsmd = rs.getMetaData();
        h = rsmd.getColumnCount();
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return h;
}

public Vector getalldata(int b)
{
    int d = b;
    String j = null;
    Vector rr  = new Vector();
    Vector v = new Vector(d);
    try
    {
        Statement s = c.createStatement();
        ResultSet r = s.executeQuery("select * from data");

        while(r.next())
        {
            v.add(r.getInt(1));
            v.add(r.getInt(2));
            v.add(r.getInt(3));
            v.add(r.getInt(4));
            v.add(r.getInt(5));
            v.add(r.getInt(6));
            v.add(r.getInt(7));
            v.add(r.getInt(8));
            v.add(r.getInt(9));
            v.add(r.getInt(10));
            v.add(r.getInt(11));
            v.add(r.getString(12));
        }
        rr.add(v);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return rr;
}

public int getrwcnt()
{
    ResultSet r;
    int j = 0;
    try
    {
        Statement s = c.createStatement();
        r = s.executeQuery("select count (*) from data");
        r.next();
        j = r.getInt(1);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return j;
}

当我运行此代码时,只有第一个结果显示在 中JTable,但是当我调用function d10.getrwcnt();它时,它会以行的形式给我输出79。我的代码有什么问题?

4

3 回答 3

1

您的问题是getalldata方法循环。

Vector v = new Vector(d);
    try {

        Statement s = c.createStatement();
        ResultSet r = s.executeQuery("select * from data");

        while(r.next()) 
        //Every time when this loop gets executed you are adding
        //data to the same vector v, instead one instance depicts one row in database,
        //so you will have to create a new instance everytime.
        {
            v = new Vector(d); //create an instance for every row in database.
            v.add(r.getInt(1));
            v.add(r.getInt(2));
            v.add(r.getInt(3));
            v.add(r.getInt(4));
            v.add(r.getInt(5));
            v.add(r.getInt(6));
            v.add(r.getInt(7));
            v.add(r.getInt(8));
            v.add(r.getInt(9));
            v.add(r.getInt(10));
            v.add(r.getInt(11));
            v.add(r.getString(12));
            rr.add(v); // add the values here
        }



    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-05-30T08:24:34.097 回答
1

移动第一行和最后一行,

    Vector v = new Vector(d);
    .... 
    while(r.next()) {
        v.add(r.getInt(1));
        v.add(r.getInt(2));
        ...
        v.add(r.getString(12));
    }

    rr.add(v);

在循环内部,如下所示,

    while(r.next()) {
        Vector v = new Vector(d);
        v.add(r.getInt(1));
        v.add(r.getInt(2));
        ...
        v.add(r.getString(12));

        rr.add(v);
    }

此外,我可以看到你的代码很 FUBAR,请不要介意。你可以做几件事,

  • 适当处理连接,尝试使用一些连接池 API
  • resultSet使用后关闭
  • 停止使用VectorArrayList改用,尤其是当 Vector 没有任何用途时
  • 尽量遵守 Java 命名约定
  • 使用有意义的变量,not b,gj
于 2012-05-30T08:27:45.373 回答
1

只有点,因为你没有搜索关于以下的内容

  • Vector 的工作原理

  • JTable 的工作原理

  • LayotManagers 的工作原理

  • 阅读相关问题(右下角)

  • 不要重新发明轮子,搜索 ResultSetTableModel 或 TableFromDatabase

  • 您对 Swing 的并发性有疑问

于 2012-05-30T08:47:26.653 回答