gpt4 book ai didi

JAVA - 将mysql结果放入表中

转载 作者:行者123 更新时间:2023-11-29 03:04:58 24 4
gpt4 key购买 nike

<分区>

我是 Java 的新手,我正在尝试制作一个带有表格 View 的窗口。在此窗口中,我想删除、更新和插入数据等。

最后看起来是这样的:

enter image description here

在任何人提示或指向好的教程的链接之前,我从未这样做过。我不是要你为我编写代码,而是要学习如何制作一个动态表,我可以在执行操作后重新加载该表。我找不到任何好的教程。我的 Java 书也没有给出很好的例子。

在 PHP 中我可以在 5 分钟内完成,但在 JAVA 中我不知道!无论如何,我会尝试用我所掌握的知识弄清楚如何做到这一点。

我现在有了这段 Java 代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class test extends JPanel {
private boolean DEBUG = false;

public test() {
super(new GridLayout(1,0));

Database db = new Database();

String tabel = "testtabel";

ResultSet res = db.query("SELECT COUNT( * ) FROM `"+tabel+"`");
ResultSet res2 = db.query("SELECT COUNT( * ) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+tabel+"'");

int rows = 0;
int collums = 0;

try {
res.next();
res2.next();

rows = res.getInt(1);
collums = res2.getInt(1);

} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

ResultSet clom = db.query("DESCRIBE `"+tabel+"`");

String[] columnNames = new String[collums];

int s = 0;

try {
while(clom.next()){
columnNames[s] = clom.getString(1);
s++;
}
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

Object[][] data = new Object[rows][collums];

ResultSet result = db.query("SELECT * FROM `"+tabel+"`");

int q = 0;
try {
while(result.next()){
for(int a=0; a<= (collums - 1); a++){
data[q][a] = result.getString(a + 1);
System.out.println(q + " - " + a);
}
q++;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
test newContentPane = new test();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

我如何使它更简单?

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