Results 1 to 4 of 4
  1. #1
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default how to take query results to a jlist!!

    Hi I do know if Im doing the right thing but I am returning my queries results to a JList in my MySQL databse editor GUI prg but its not working how do you achieve this and by the way do you use a JList??
    The dream of being a legend has just begun!

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    by the way do you use a JList??
    It depends,

    I am returning my queries results to a JList in my MySQL databse editor GUI prg but its not working how do you achieve this
    The problem maybe on the values that you've receive, or showing it to JList...

    Can you show the query results without using JLIst?

    if so, then your problem is on how you add such values into the JLIst....

    Take some experiments on JList with DefaultListModel
    freedom exists in the world of ideas

  3. #3
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default Sorry its a JTextArea not a JList

    Ooh sorry about that I'm using a JTextArea not the JList.

    The query should return two columns name and owner
    e.g something like

    name owner
    Fluffy mburu
    George njoks
    Dan nini

    I tried using a StringBuffer which I append to the JTextArea. How do you do this, I will submit the code later.
    The dream of being a legend has just begun!

  4. #4
    themburu is offline Member
    Join Date
    Mar 2008
    Posts
    20
    Rep Power
    0

    Default the code

    here's ma code its from netbeans.

    Java Code:
    /*first i'm using two Jbuttons,
     *the first buttons reads the selected database to connect to from a JComboBox; 1 being MySql
     *	it thus retrives the username, passwd and database to connect and executes the connection
     *
     *the second is to run the entered queries but for development purposes I have had to define a standard query
     *this query should return results as shown in previous thread but now in a JTextArea called myans.
     */
    
    
    private void mycActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mycActionPerformed
    
           String myname = new String(""+ uname.getText()) ;
           String mypasswd = new String(""+ passwd.getText()) ;
           String mydb = new String(""+ dbname.getText()) ;
    
    //Ceck if the username has been provided
    if (myname!=""){
        //parameters to connect to the database
        int chos = mychoices.getSelectedIndex();
    
        if (chos == 1){ 
            try {
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
            
            String url = "jdbc:mysql://localhost/"+mydb;
    
            java.sql.DriverManager.getConnection( url, myname, mypasswd ); 
            	
            }
          catch( Exception x ) {
          x.printStackTrace();  
          	 } 
            
         
            
            } 
        else if (chos == 2){
        JOptionPane.showMessageDialog(this,"You are not allowed to connect to PostgeSQL","Configuration Error",JOptionPane.ERROR_MESSAGE);
        } 
        else if (chos == 3){
        JOptionPane.showMessageDialog(this,"You are not allowed to connect to Oracle","Configuration Error",JOptionPane.ERROR_MESSAGE);
        } 
      }
       
    //If no username show error!    
    else if (myname == ""){
     JOptionPane.showMessageDialog(this,"Please give atlease a user name","ENTRY PROBLEM!",JOptionPane.ERROR_MESSAGE);
      }   
        }
      
      
      
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    
    myans.setText("Here comes the results \n");
    StringBuffer message = new StringBuffer("");
    Connection conn = null;
    Statement stmt = null;
    try{
                //StringBuffer message = new StringBuffer("");
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT name, owner FROM pet");
    
                //ResultSetMetaData metaData = resultSet.getMetaData();
                //int numberOfColumns = metaData.getColumnCount(); 
                
                ResultSetMetaData metaData = rs.getMetaData();
                int numberOfColumns = metaData.getColumnCount();
    
                myans.setText("Column count is :" + numberOfColumns +"\n");
    
               /* while ( rs.next() )
                    {
                    for ( int i = 1; i <= numberOfColumns; i++ )
                   myans.setText("Column count is :" + rs.getObject(i) +"\n");
                   //message.append(rs.getObject(i));
                   //message.append("\n");
                     }*/
                
                while (rs.next()) {
                //String entry = rs.getString(1);
                String s = rs.getString("name");
                message.append(s);
                String f = rs.getString("owner");
                message.append(f);
                }
            String hdss = message.toString();
            myans.append(hdss);
            //myans.setText(hdss);
                
                } 
            catch (SQLException e) {
                e.printStackTrace();
                System.exit( 1 );
                } 
    finally // ensure statement and connection are closed properly
       {                                                             
       try  {                                                          
           stmt.close();                                      
           conn.close();                                     
           } // end try                                               
     catch ( Exception exception )  {                                                          
          exception.printStackTrace();                            
          System.exit( 1 );                                       
           } // end catch                                             
            } // end finally 
        }
    i've also tried to make sure that the username is not null and give an error message, I've used if with !="" and !=null but none works.

    For the Jtextarea i've even tried StringBuffer.
    So pls help me.
    The dream of being a legend has just begun!

Similar Threads

  1. how to take query results to a jlist!!
    By themburu in forum JDBC
    Replies: 1
    Last Post: 06-07-2008, 11:51 AM
  2. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 05:24 AM
  3. date and calender not getting the right results
    By valoyivd in forum New To Java
    Replies: 4
    Last Post: 04-14-2008, 12:51 PM
  4. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:07 PM

Posting Permissions

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