int num = inInfo.getBlock("result").getRowCount();什么意思
时间: 2023-07-14 18:12:44 浏览: 37
这行代码的含义是从一个名为 `inInfo` 的对象中获取名为 `result` 的块,并返回该块中的行数,赋值给整型变量 `num` 。这段代码可能用于读取某个数据表格或者类似结构的数据,并获取其中数据行的数量。具体实现方式取决于 `inInfo` 对象的类型和 `getBlock()` 方法的具体实现。
相关问题
public void searchAndDisplay(String searchText, JTable table, JTable resultTable) { TableModel model = table.getModel(); int rowCount = model.getRowCount(); int colCount = model.getColumnCount(); DefaultTableModel resultModel = new DefaultTableModel(); //resultModel.setColumnIdentifiers(UserTable.getColumnIdentifiers()); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { if (searchText.equals(model.getValueAt(i, j).toString())) { Object[] rowData = new Object[colCount]; for (int k = 0; k < colCount; k++) { rowData[k] = model.getValueAt(i, k); } resultModel.addRow(rowData); } } } resultTable.setModel(resultModel); } private void UserSearchActionPerformed(ActionEvent evt) { // TODO Auto-generated method stub String UserName = this.UserNameTxt.getText(); int column = 4; for(int row = 0;row< UserTable.getRowCount();row++) { Object value = UserTable.getValueAt(row, column); if(value.equals(UserName)) { DefaultTableModel dtm = (DefaultTableModel) UserTable.getModel(); dtm.setRowCount(0); /*TableRowSorter<TableModel> sorter = new TableRowSorter<>(model); sorter.setRowFilter(RowFilter.regexFilter(UserName)); UserTable.setRowSorter(sorter);*/ }else { JOptionPane.showMessageDialog(null, "该用户不存在"); return; } } }为我的代码添加一个查找功能:当我输入用户姓名时,它能从json文件中找到相应的数据并输出特定行
好的,那么您想要的功能可以通过以下步骤来实现:
1. 首先需要从json文件中读取数据,将其转化为Java对象。
2. 然后遍历Java对象,查找符合条件的数据行。
3. 最后将符合条件的数据行添加到新的TableModel中,并将其设置到显示结果的JTable中。
以下是示例代码,供您参考:
```
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.google.gson.Gson;
public class JsonSearchDemo {
public static void main(String[] args) {
// 示例代码中使用了Gson库来解析json文件,您可以根据实际情况选择其他库或自行解析。
Gson gson = new Gson();
try (FileReader reader = new FileReader("data.json")) {
// 将json数据读取为User对象列表
User[] users = gson.fromJson(reader, User[].class);
// 构造显示数据的TableModel
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("ID");
tableModel.addColumn("Name");
tableModel.addColumn("Age");
for (User user : users) {
Object[] row = {user.getId(), user.getName(), user.getAge()};
tableModel.addRow(row);
}
// 创建显示数据的JTable
JTable table = new JTable(tableModel);
// 创建查找结果的JTable
JTable resultTable = new JTable();
// 调用查找函数
searchAndDisplay("Alice", users, resultTable);
// 显示查找结果
JOptionPane.showMessageDialog(null, resultTable);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void searchAndDisplay(String searchText, User[] users, JTable resultTable) {
DefaultTableModel resultModel = new DefaultTableModel();
resultModel.addColumn("ID");
resultModel.addColumn("Name");
resultModel.addColumn("Age");
for (User user : users) {
if (user.getName().equals(searchText)) {
Object[] row = {user.getId(), user.getName(), user.getAge()};
resultModel.addRow(row);
}
}
resultTable.setModel(resultModel);
}
}
class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
请注意,示例代码中的User对象只包含了id、name和age三个属性,您需要根据自己的实际情况来修改代码。此外,示例代码中使用了JOptionPane来显示查找结果,您也可以将其修改为更适合自己的方式。
if (e.getSource() == delete){ try { PreparedStatement statement = connection.prepareStatement("DELETE FROM ade WHERE remark=" + "'" + DnoText.getText() + "'"); statement.executeUpdate(); PreparedStatement state = connection.prepareStatement("select*from ade"); ResultSet resultSet = state.executeQuery(); while (mm.getRowCount() > 0) {//把表格进行刷新,下次显示的时候重头开始显示 //System.out.println(model.getRowCount()); mm.removeRow(mm.getRowCount() - 1); } while (resultSet.next()) { String Sno=resultSet.getString(1); String Sname=resultSet.getString(2); String Ssex=resultSet.getString(3); String Sdept=resultSet.getString(4); String Dno=resultSet.getString(5); String[] data={Sno,Sname,Ssex,Sdept,Dno}; mm.addRow(data); } } catch (SQLException e1) { e1.printStackTrace(); } JOptionPane.showMessageDialog(this, "删除成功"); }对这段代码进行详细注释
这段代码是在按钮被点击时执行的,用于从数据库中删除一条记录,并更新表格的显示。下面是对这段代码的详细注释:
```java
if (e.getSource() == delete) { // 如果点击的是删除按钮
try {
// 创建 PreparedStatement 对象,用于执行 SQL 语句
PreparedStatement statement = connection.prepareStatement("DELETE FROM ade WHERE remark=" + "'" + DnoText.getText() + "'");
// 执行 SQL 语句,删除指定的记录
statement.executeUpdate();
// 创建 PreparedStatement 对象,用于查询数据库中的所有记录
PreparedStatement state = connection.prepareStatement("SELECT * FROM ade");
// 执行查询操作,获取查询结果集
ResultSet resultSet = state.executeQuery();
// 清空表格中的数据
while (mm.getRowCount() > 0) {
mm.removeRow(mm.getRowCount() - 1);
}
// 将查询结果添加到表格中
while (resultSet.next()) {
String Sno = resultSet.getString(1);
String Sname = resultSet.getString(2);
String Ssex = resultSet.getString(3);
String Sdept = resultSet.getString(4);
String Dno = resultSet.getString(5);
String[] data = {Sno, Sname, Ssex, Sdept, Dno};
mm.addRow(data);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(this, "删除成功"); // 弹出删除成功的提示框
}
```
此代码首先检查事件源是否为删除按钮。如果是,则创建 PreparedStatement 对象,用于删除数据库中的一条记录,其中 SQL 语句的参数是从 DnoText 文本框中获取的备注信息。然后执行 SQL 语句,删除指定的记录。接下来创建另一个 PreparedStatement 对象,用于查询数据库中的所有记录,并执行查询操作,获取查询结果集。然后清空表格中的数据,并将查询结果添加到表格中。最后弹出一个提示框,提示删除成功。如果执行过程中出现异常,将打印堆栈跟踪信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)