【发布时间】:2016-09-29 08:19:33
【问题描述】:
基本上,这是我的一小段代码,允许用户通过名称搜索名为“艺术家”的数据库以查找记录。我将如何显示检索到 JTable 的信息?
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");/* loads the jdbcodbc driver not using username and password */
Connection connect = DriverManager.getConnection("jdbc:odbc:artist");
Statement state = connect.createStatement();/* Gets a statement */
String query = "SELECT * FROM Artists "+ "WHERE Name = '" + txtName.getText() + "'";
ResultSet results = state.executeQuery(query);/* Result set returned for a query */
if (!results.next()) {
System.out.println("Name is incorrect");
throw new WrongNameException();/* Exception thron if information is incorrect*/
} else {
System.out.println("You have successfully Searched!");
}
state.close();
} catch(SQLException | ClassNotFoundException | WrongNameException e) { /* catches the exceptions */
JOptionPane.showMessageDialog(null,e,"Error ",0);
}
}
【问题讨论】:
-
this post 的可能重复项?
-
我有一个可能的解决方案。
标签: java database swing resultset