gpt4 book ai didi

java - 使用netbeans从jtable中的mysql检索数据

转载 作者:行者123 更新时间:2023-11-29 11:20:31 25 4
gpt4 key购买 nike

我正在尝试将数据从数据库检索到 jTable 中。我使用 NetBeans 8.1。我无法让它发挥作用。

我使用此代码插入:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = jTextField1.getText();
String father = jTextField2.getText();
String mother = jTextField3.getText();
String line1 = jTextField4.getText();
String line2 = jTextField5.getText();
String line3 = jTextField11.getText();
String roll = jTextField6.getText();
String school = jTextField7.getText();
String hobby1 = jTextField8.getText();
String hobby2=jTextField9.getText();
String hobby3 = jTextField10.getText();
String address = line1 + " " + line2 + " " + line3;
//String hobby = hobby1 + " " + hobby2 + " " + hobby3;
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("dd-MM-yyyy");
String date = fmt.format(jDateChooser1.getDate());
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/pritam", "root", "root");
PreparedStatement ps = con.prepareStatement(
"insert into basic_info values(?,?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, father);
ps.setString(3, mother);
ps.setString(4, roll);
ps.setString(5, date);
ps.setString(6 ,address);
// ps.setString(7, hobby);
ps.setString(7, school);
ps.executeUpdate();
con.close();
}catch(Exception E) {
System.out.println(E);
}

如何检索 jTable 中的数据?

最佳答案

您需要将 AbstractTableModel 实现为:(我只是在这里给出一个示例)

NameTableModel.java

public class NameTableModel extends AbstractTableModel {

String[] columnNames = {"Id", "Name","Father", "Mother","Hobby" };
Object[][] data;

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}

NameStoreUI.java

public class NameStoreUI extends javax.swing.JPanel {

public NameStoreUI() {
initComponents();
loadData();
}
void loadData()
{

NameTableModel model = new NameTableModel();
NameDAO m = new NameDAO();
Object[][] data_x = m.list();
model.data = data_x;
tableData.setModel(model);
}

NameDAO.java

public Object[][] list()
{
try
{
s = con.createStatement();
rs = s.executeQuery("SELECT * FROM name_table");

ArrayList mylist = new ArrayList();

int column_count = rs.getMetaData().getColumnCount();

while (rs.next())
{
String[] s = {rs.getString("id"),rs.getString("name"),rs.getString("father"),rs.getString("mother"),rs.getString("hobby")};
mylist.add(s);
}

Object[][] o = new Object[mylist.size()][column_count];

int i, j;
for(i = 0 ; i < mylist.size() ; i++)
{
o[i] = (String[])mylist.get(i);
}

return o;

}
catch(SQLException s)
{
System.out.println("Error in list()");
}
return null;
}

我希望你明白了。

关于java - 使用netbeans从jtable中的mysql检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006332/

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