Search code examples
javamysqlsqlitejdbcjcombobox

Sqlite Column Determination Error


I'm trying to get selection from J combo Box and use that to find table from the data base. But instead an error comes up:

The thing that shows

My codes it:

JButton btnGo = new JButton("Go!");
    btnGo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Connection conni = null;
            ResultSet rs=null;
            PreparedStatement pst=null;
            try{
                Class.forName("org.sqlite.JDBC");
                conni  = DriverManager.getConnection("jdbc:sqlite://C://Users//Asus//Dropbox//TireShop.sqlite");
                String x = comboBox.getSelectedItem().toString();
                String sql="select * from " + x;


                pst=conni.prepareStatement(sql);
                rs=pst.executeQuery();
                while(rs.next()){
                    String name = rs.getString("Namet");

                    nameofguy.setText(rs.getString(name));
                }

            }catch(Exception i){
                JOptionPane.showMessageDialog(null, i);

            }

I'm searching for table.. but it says cannot find column.


Solution

  • Here's your problem

    while(rs.next()){
       String name = rs.getString("Namet");
       //rs.getString returns Ayaan. So value of name is "Ayaan"
       nameofguy.setText(rs.getString(name));
       // Youre trying to get the value corresponding to the column Ayaan here
       //This is why the exception is thrown as there is no column called Ayaan
       }
    

    Instead do

    while(rs.next()){
       String name = rs.getString("Namet");
       nameofguy.setText(name);
       }