所以我有一个带字符串参数的字符串方法。我试图通过文本字段(简单部分)将字符串传递给方法,并对该传递的参数执行SQL查询。它是一个RMI应用程序,这个方法的工作是检索有关项目的详细信息。
public String SearchItem(String item) throws RemoteException {
String olo1 = "Select * from StockTB where ItemName ='" + item + "';";
ArrayList<String> a = new ArrayList<String>();
try{
Statement statement = link.createStatement();
ResultSet results = statement.executeQuery(olo1);
while(results.next()){
System.out.println("Item_ID:" + results.getInt(1));
System.out.println("Item Name:" + results.getString(2));
System.out.println("Description:" + results.getString(3));
System.out.println("Price:" + results.getString(4));
System.out.println("Stock Amount:" + results.getString(5));
System.out.println("Item found!");
return("Item found!");
}
}
catch(Exception e){
System.out.println("Error: Error retrieving data!");
e.printStackTrace();
System.exit(1);
}
try {
link.close();
}
catch(SQLException e1){
System.out.print("Error: Unable to disconnect!");
e1.printStackTrace();
}
System.out.println("Item not found!");
return("Not found");
}
这是我在另一个班级的按钮代码。
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
Registry reg = LocateRegistry.getRegistry("192.168.0.10",1099);
db123 = (DBService)Naming.lookup("rmi://localhost/DBService");
String tt2 = db123.SearchItem(textField1.getText());
if(tt2 != null){
//tt2.
}
else{
JOptionPane.showMessageDialog(null,textField1.getText() + " Item not found! Please search for another item.");
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null, e1);
}
}
});
我要做的是通过GUI中的第一个文本字段输入项目,并将结果返回到后续的文本字段中。我很难过。我一直在看resultSet对象,但无法搞清楚。只是想获得想法。