切换表列以输入数据

时间:2014-05-12 18:15:38

标签: java mysql swing jtable

我正在开发一个项目,我从中选择mysql表中的数据(用户选择要获取数据的表)。我使用以下代码从选定的表中获取数据:

jTextArea1.setText(null);
tableModel.setColumnCount(0);
tableModel.setRowCount(0);
try {
    try {
        Class.forName("java.sql.DriverManager");
        timeget();
        jTextArea4.append(now + ":   " + "/ Driver name Detected successfully / \n \n");
    } catch (ClassNotFoundException e) {
        timeget();
        jTextArea4.append(now + ":   " + "/ Failed in finding Driver \n " + e.getMessage() + " / \n It is recommended that you DO NOT PROCEED ANY FURTHER \n \n");
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
    con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:" + GlobalParams.portvar + "/" + (String) jList1.getSelectedValue(), "" + GlobalParams.uservar, "" + GlobalParams.passvar);
    Statement stmnt = (Statement) con.createStatement();
    String query1 = "Use " + GlobalParams.dbvar;
    stmnt.executeQuery(query1);
    String query2 = "desc " + (String) jList2.getSelectedValue();
    ResultSet rs = stmnt.executeQuery(query2);
    while (rs.next()) {
        fieldo = rs.getString("Field");
        jTextArea1.append(fieldo + "\n");
    }
    tableModel.addColumn(jTextArea1.getText().substring(0, jTextArea1.getText().indexOf("\n")));
    Scanner scanner = new Scanner(jTextArea1.getText());
    timeget();
    jTextArea4.append(now + ":   " + "/ Scanning Available feeds from table '" + (String) jList2.getSelectedValue() + "' / \n");
    timeget();
    jTextArea4.append(now + ":   " + "/ Getting feeds from table '" + (String) jList2.getSelectedValue() + "' / \n \n");
    if (scanner.hasNextLine()) {
        scanner.nextLine();
        while (scanner.hasNextLine()) {

//I BELIEVE MY CODE STARTS TO GO SOMEWHERE WRONG HERE BELOW:

            String line = scanner.nextLine();
            tableModel.addColumn(line);
            Statement stmnt2 = (Statement) con.createStatement();
            String tdataget = "Select " + line + " from " + (String) jList2.getSelectedValue();
            ResultSet rs2 = stmnt2.executeQuery(tdataget);
            while (rs2.next()) {
                String field = rs2.getString(line);
                tableModel.addRow(new Object[]{field});
            }
            int end = jTextArea1.getLineEndOffset(0);
            jTextArea1.replaceRange("", 0, end);
        }
    }
} catch (SQLException e) {
    if (!(e.getMessage().contains("You have an error in your SQL syntax"))) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
} catch (BadLocationException ex) {
    JOptionPane.showMessageDialog(this, ex.getMessage());
}

(jList2是取自表名的地方,jList1包含数据库名称)

问题是,例如。如果mysql中的表看起来像这样 - >

Col1    |    Col2    |    Col3    |    Col4    |    Col5
---------------------------------------------------------
Data1   |    Data2   |    Data3   |    Data4   |    Data5
Datab1  |    Datab2  |    Datab3  |    Datab4  |    Datab5

在我的框架中,该表格显示为this->

Col1    |    Col2    |    Col3    |    Col4    |    Col5
---------------------------------------------------------
Data1

Data2

Data3

Data4

Data5

Datab1

Datab2

Datab3

Datab4

Datab5

那么有人可以建议我在java中“切换到下一列”的代码吗? 感谢

1 个答案:

答案 0 :(得分:1)

tableModel.addRow(new Object[]{field});

您正在添加一个包含始终具有单个值的Array的行。相反,您需要添加一个包含该行的所有值的数组。

查看Table From Database中的TableFromDatabaseExample.java代码,了解如何执行此操作的更一般方法。