我不知道什麼是錯,此代碼; SQL命令和路徑看起來沒問題。連接到數據庫是好的;但是當我試圖通過一個簡單的搜索;它返回那個表不存在。 我試過(相同的代碼)在不同的方法來添加數據,它工作正常;所以我不知道我做錯了什麼。嘗試連接到一個SQLite而是:SQL錯誤或丟失的數據庫(沒有這樣的表:Bank_001)
感謝您的關注。
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
First_GUI frame = new First_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection conn = null; //Call the Connection Class from JavaSqlite Class
/**
* Create the frame.
*/
public First_GUI() {
conn = Sqlite_Connection.dbconnector(); //call the connection CONN from SQLite class, method DBconnector.
setTitle("First GUI App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblEnterTheName = new JLabel("Enter the Name :");
lblEnterTheName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEnterTheName.setVerticalAlignment(SwingConstants.TOP);
lblEnterTheName.setHorizontalAlignment(SwingConstants.LEFT);
panel.add(lblEnterTheName);
NameField = new JTextField();
panel.add(NameField);
NameField.setColumns(10);
JButton Search = new JButton("Search");
Search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// ****************************************************************
// Here is the PROBLEM.
String sql = "SELECT * FROM Bank_001 where AccountName like ?";
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, NameField.getText());
ResultSet rs = pst.executeQuery();
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
});
panel.add(Search);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextPane textPane = new JTextPane();
textPane.setFont(new Font("Tahoma", Font.PLAIN, 14));
scrollPane.setViewportView(textPane);
}
}
表Bank_001是否確實存在於您的數據庫中? – firelynx
是的! Table Bank_001有4列7行。 –
您的代碼使用的數據庫用戶是否可以訪問表Bank_001? – firelynx