I have JList and the JList contain names of fields of table in the db
this is my code. I'm stuck in writing the query I understand that the sql query should be like this "SELECT column_name1,column_name2..etc FROM" however now I got the column formated this // System.out.print(aa + bb);' will print the column names seprated by comma , but I don't know where to put the *SELECT* and *FROM* when this >>System.out.print(( "SELECT " + all +" FROM "));` excexeted I get this ouput SELECT column_name1, FROM SELECT column_name2, FROM SELECT column_name3, FROM .. etc I'm not sure which part of my code is wrong (will I need stack , linkedList or arrayList or none) can some one please help me

    String selectedTable = ComboTables.getSelectedItem().toString();
        ListModel a = list2.getModel();
        String obj = null;
        String obj2 = null;
        for (int i = 0; i < a.getSize(); i++) {
            try {
                if (i == a.getSize() - 1) {
                    obj = (String) a.getElementAt(i);
                } else {
                    obj2 = a.getElementAt(i) + ",";
                }
                String aa = obj2;
                String bb = "";
                if (obj != null) {
                    bb = obj;
                }

                String all = aa + bb;
               // System.out.print(aa + bb);
                System.out.print(( "SELECT " + all +" FROM "));

                Connection conn = ConnectionManager.MyConn();
                PreparedStatement q = conn.prepareStatement("SELECT " + all +" FROM");
                 ResultSet r = q.executeQuery();
                while (r.next() != false) {
                   // String myTablesName = rs.getString(1);


                 }
            } catch (SQLException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.print(ex.getMessage());

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.print(ex.getMessage());

            } catch (InstantiationException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.print(ex.getMessage());

            } catch (IllegalAccessException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.print(ex.getMessage());

            }

        }

Recommended Answers

All 2 Replies

^
^
^
^
^
^

These lines:

// System.out.print(aa + bb);
System.out.print(( "SELECT " + all +" FROM "));

are inside your loop, so for each column you are selecting on, it adds "SELECT column-name, FROM" to your query string, hence the malformed request. Set the query string to "SELECT " before the loop, and add a comma on all but when "i == 0", and the column-name on each loop, and " FROM table-name..." after the loop. Also, set the PreparedStatement q ... after the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.