你好我正在为我的java类做一个项目,简单地说,我必须做一个程序,让你手工查询,就像我把一个文本框的例子“select * from table”,它显示结果,第二个问题是如果删除,更新或插入程序应该自动执行选择句子来显示结果。在jtable中显示quer
public class InfoCd extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField textQuery;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InfoCd frame = new InfoCd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection conn=null;
/**
* Create the frame.
*/
public InfoCd() {
conn=sqliteConnection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 753, 477);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.activeCaption);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnHacerConsulta = new JButton("Hacer Consulta");
btnHacerConsulta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query="'?'";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, textQuery.getText());
ResultSet rs=pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
btnHacerConsulta.setBounds(112, 388, 169, 39);
contentPane.add(btnHacerConsulta);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 737, 367);
contentPane.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
textQuery = new JTextField();
textQuery.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 11));
textQuery.setHorizontalAlignment(SwingConstants.CENTER);
textQuery.setText("Escribir Consulta Aqui");
textQuery.setToolTipText("");
textQuery.setBounds(422, 388, 270, 39);
contentPane.add(textQuery);
textQuery.setColumns(10);
}
}
问题是什么? – ravthiru
当我把querry放在文本框上,然后点击接受程序崩溃,我相信这不起作用 'String query =“'?'”; PreparedStatement pst = conn.prepareStatement(query); pst.setString(1,textQuery.getText()); ResultSet rs = pst.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(rs)); }' – Nycoh
'并点击接受程序崩溃 - - 这告诉我们什么都没有。我想你会得到某种信息。你会如何期待一个由“?”组成的SQL语句会做任何事情?这不是如何使用SQL语句。事实上,如果你期望用户输入整个语句,那么你甚至不应该使用PreparedStatement。用户可以正确地格式化语句,顺便说一下,这不是一个很好的设计。基本上变量“查询”需要从文本字段中分配文本。 – camickr