vendredi 10 juillet 2015

Trying to connect to a sqlite but: SQL error or missing database (no such table: Bank_001)

I don't know what is wrong with this code; the SQL commands and the path looks alright. The connect to the db is ok; but when I try to pass a simple search; it returns that that table doesn't exist. I tried it (the same code) in a different method to add data and it works fine; so I don't know what I'm doing wrong.

Thanks for your attention.

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);


}

}

enter image description here

Aucun commentaire:

Enregistrer un commentaire