我正在学习JDBC,这是一个全新的知识。我非常了解MySQL。我的小程序中有一个搜索表单(选项卡2),在其中搜索食物。当我单击搜索时,我希望它从数据库中搜索确切的食物,并仅返回该特定行的表内容。但是我没有任何结果。仅打开一个空框架。但是,当我将查询输入为“ SELECT * FROM table”时,我将获得完整的表。但是我只需要搜索的那一行。谁能告诉我我要去哪里错了?这是我的代码:
import java.awt.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.util.Vector;
import javax.swing.*;
import com.mysql.jdbc.*;
public class Tryout extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
final Vector columnNames = new Vector();
final Vector data = new Vector();
private JTabbedPane tabbedPane = new JTabbedPane();
private JPanel inputpanel;
private JPanel searchpanel;
public String s;
public Tryout() {
inputpanel = createPage1();
searchpanel = createPage2();
tabbedPane.addTab("Input Form", inputpanel);
tabbedPane.addTab("Search Form", searchpanel);
this.add(tabbedPane, BorderLayout.CENTER);
}
public JPanel createPage1() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//Some code
return panel;
}
public JPanel createPage2() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.5;
c.weighty = 0.5;
JLabel region = new JLabel("Enter Region");
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 0;
c.gridy = 0;
panel.add(region, c);
JTextField field = new JTextField(20);
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 1;
c.gridy = 0;
panel.add(field, c);
JButton search = new JButton("SEARCH");
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 2;
c.gridy = 0;
panel.add(search, c);
s = field.getText();
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/delikat", "root", "");
PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT * FROM delicious WHERE name = ?");
statement.setString(1,s);
ResultSet rs= (ResultSet) statement.executeQuery();
ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
statement.close();
}
catch(Exception e) {}
JFrame tab=new JFrame();
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
tab.add( scrollPane );
tab.setVisible(true);
tab.setSize(300,100);
}
});
return panel;
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Tryout ex = new Tryout();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.setSize(500,500);
ex.setVisible(true);
}
});
}
}
这是我的桌子:
另外,我正在尝试在搜索表单下方的面板中显示表格,但添加时却显示错误
panel.add(scrollpane);
我也该如何解决这个问题?
谢谢
最佳答案
基本调试。您是否显示了PreparedStatement中使用的“ s”值?
s = field.getText();
据我所知,该语句是在构建GUI时执行的,这没有任何意义,因为用户甚至没有在此文本字段中输入任何文本。
实际调用SQL时需要执行该语句。