Results 1 to 2 of 2
  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 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
    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. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 05:24 AM
  2. 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
  3. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:07 PM
  4. results to code disappear too fast for DOS window
    By dubdubdub in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 06: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
  •