【发布时间】:2013-12-13 02:37:32
【问题描述】:
我有一个搜索按钮,可以进入我的数据库并搜索一个名称,但是当有两个相同名称的条目时,它只会让我返回一个,我不知道为什么。下面是我现在使用的代码。
public boolean search(String str1, String username){//search function for the access history search with two parameters
boolean condition = true;
String dataSourceName = "securitySystem";//setting string datasource to the securitySystem datasource
String dbUrl = "jdbc:odbc:" + dataSourceName;//creating the database path for the connection
try{
//Type of connection driver used
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection variable or object param: dbPath, userName, password
Connection con = DriverManager.getConnection(dbUrl, "", "");
Statement statement = con.createStatement();//creating the statement which is equal to the connection statement
if (!(username.equals(""))){
PreparedStatement ps = con.prepareStatement("select * from securitysystem.accessHistory where name = ?");//query to be executed
ps.setString(1, username);//insert the strings into the statement
ResultSet rs=ps.executeQuery();//execute the query
if(rs.next()){//while the rs (ResultSet) has returned data to cycle through
JTable table = new JTable(buildTableModel(rs));//build a JTable which is reflective of the ResultSet (rs)
JOptionPane.showMessageDialog(null, new JScrollPane(table));//put scrollpane on the table
}
else{
JOptionPane.showMessageDialog(null,"There has been no system logins at this time");// else- show a dialog box with a message for the user
}
}
statement.close();//close the connection
} catch (Exception e) {//catch error
System.out.println(e);
}
return condition;
}
【问题讨论】:
-
您使用的是
if而不是while。如果您正在循环结果集,请显示buildTableModel -
"buildTableModel(...)到底做了什么?这种方法在这里是不是有点关键? -
因为你不显示
buildTableModel()没有人可以帮助你。 -
如需尽快获得更好的帮助,请发帖SSCCE。
标签: java swing jdbc jtable resultset