Results 1 to 2 of 2
  1. #1
    NeVerEveR is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default How to display queries results from Oracle according to user input?

    Java Code:
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import javax.swing.JTextArea;
    
    
    public class execute_sql extends JApplet{
    
        private JTextArea sql_query = new JTextArea ("Type in your query statements here.",15, 20);
        private JButton enter = new JButton("Submit Query");
        private JTextArea output = new JTextArea ( 5, 20);
        private String results ="";
        private Statement stmt;
        
    
        public void init(){
            initializeDB();
    
         enter.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
                    try {
                        enter_actionPerformed(e);
                    } catch (SQLException ex) {
                        Logger.getLogger(execute_sql.class.getName()).log(Level.SEVERE, null, ex);
                    }
                   
          }
        }
          );
    
        JPanel p1 = new JPanel(new GridLayout(2,1));
        p1.setBorder( new TitledBorder("Query Statements"));
        sql_query.setLineWrap(true);
        sql_query.setWrapStyleWord(true);
        sql_query.selectAll();
    
        JScrollPane scrollPane_query = new JScrollPane(sql_query);
    
        p1.add(scrollPane_query);
        p1.add(enter);
    
        JPanel p2 = new JPanel();
        p2.setBorder( new TitledBorder("Query Results"));
        output.setLineWrap(true);
        output.setWrapStyleWord(true);
        output.setEditable(false);
        JScrollPane scroller = new JScrollPane(output ,
                                            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        
        p2.add(scroller);
        
        setLayout(new BorderLayout());
        add(p1,BorderLayout.CENTER);
        add(p2,BorderLayout.SOUTH);
    
    
    
    }
    
      
        private void initializeDB() {
           
           try {
    
               // Class.forName("com.mysql.jdbc.Driver");
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //String connectionUrl = "jdbc:mysql://localhost/mysql?" +
                                       //"user=root&password=140589";
                  String connectionUrl = "jdbc:oracle:thin:hr/hr@localhost:1521:xe ";
                Connection con = DriverManager.getConnection(connectionUrl);
                
                stmt = con.createStatement();
                System.out.println("success");
            } catch (SQLException e) {
                System.out.println("SQL Exception: "+ e.toString());
            } catch (ClassNotFoundException cE) {
                System.out.println("Class Not Found Exception: "+ cE.toString());
            }
    
    
        }
    
        private void enter_actionPerformed(ActionEvent e) throws SQLException{
    
            String sql = sql_query.getText() ;
    
            ResultSet resultset = stmt.executeQuery(sql);
    
            try{
    
            while(resultset.next()){
                     
                     results = resultset.getString(1) + "\t"+ resultset.getString(2)+ "\t"+ resultset.getString(3) ;
                     output.append(results + "\n");
                }
        }
            catch( SQLException ex){
                ex.printStackTrace();
            }
    
             
    
          
          
    
        }
    }
    This is the coding for an applet which allows the users to type in any queries they want and the output text area will display the results.
    The problem is that in my Oracle daabase, there are different tables with different number of columns.

    while(resultset.next()){

    Java Code:
                     
                     results = resultset.getString(1) + "\t"+ resultset.getString(2)+ "\t"+ resultset.getString(3) ;
                     output.append(results + "\n");
                }
    So i would like to know what changes can be made to this part so that I can get the number of columns of that particular table or make it possible to display all the columns without knowing the exact number of the columns.

    For example, I receive this :
    java.sql.SQLException: Invalid column index

    when I type in SELECT* FROM DEPARTMENTS as department table only has 2 columns. whereas it work well with COUNTRIES table as it has exactly 3 columns.

    The other thing is the horizontal scroll bar. It seems to be not working. the row which cannot be fitted in one row continue on the second row.

    That's all what I wanna ask. Bear with me. Thanks a lot!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for ResultSetMetaData and use a StringBuilder and a for loop.

    As far as the line continuing on the second line, rather than being wider than the window, what do you think the following (from your code) does?
    Java Code:
    sql_query.setLineWrap(true);

Similar Threads

  1. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  2. having a JSP display the results in a Portlet setup
    By Drun in forum Web Frameworks
    Replies: 2
    Last Post: 01-27-2010, 03:36 PM
  3. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  4. Replies: 0
    Last Post: 04-21-2009, 01:32 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •