gpt4 book ai didi

java - 切换表格列以输入数据

转载 作者:行者123 更新时间:2023-11-29 12:58:59 26 4
gpt4 key购买 nike

我正在开发一个项目,我从选定的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

在我的框架中,表格如下所示->

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

Data2

Data3

Data4

Data5

Datab1

Datab2

Datab3

Datab4

Datab5

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

最佳答案

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

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

查看 Table From Database 中找到的 TableFromDatabaseExample.java 代码获取展示如何执行此操作的更通用方法。

关于java - 切换表格列以输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com