Results 1 to 2 of 2
  1. #1
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default Trouble with jtables from a database

    I'm trying to retrieve information from a database and display it in a JTable for part of my project.

    I can retrieve the information needed easily but am finding it difficult to show it in a JTable. I've looked at loads of tutorials and I can't seem to implement it into my project, In the end I kind of cheated and displayed the information into a JTextArea but this has not format to it and the information looks messy. As I'm completely new to using Swing, I downloaded Jigloo to create a GUI for my project.

    I have the following code. (I've cut it short to the bit where the data is extracted from the database)

    Java Code:
    Vector columnNames = new Vector();
     Vector data = new Vector();
    
    try {
    //Read info from database
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection (Url);
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    //read names of columns
    						
    						
    					    for (int i = 1; i <= columns; i++)
    		                  {
    					    	
    		                       columnNames.addElement( md.getColumnName(i) );
    		                      
    		                      
    		                  }
    					    System.err.println(columnNames);
    					    
    						
    					    String rowdatatotal = columnNames + "\n" + "\n";
    	                      
    					    while (rs.next())
    		                  {
    		                        Vector row = new Vector(columns);
    		 
    		                        for (int i = 1; i <= columns; i++)
    		                        {
    		                              row.addElement( rs.getObject(i) );
    		                        }
    		 
    		                        data.addElement( row );
    		                        System.out.println(row);
    		                       Vector rowdata = (row);
    								
    								rowdatatotal = rowdatatotal + rowdata + "\n" ;
    								sqloutputArea.setText (rowdatatotal);
    		                  }
    					    JTable table = new JTable(data, columnNames);
    					    
    						/*while ( rs.next ()) {
    							
    							firstString = rs.getString(1);
    							secondString = rs.getString(2);
    							thirdString = rs.getString(3);
    							fourthString = rs.getString(4);
    							fifthString = rs.getString(5);
    							sixthString = rs.getString(6);
    							seventhString = rs.getString(7);
    							
    							System.out.println(firstString + " " + secondString + " " + thirdString + " " + fourthString + " " + fifthString + " " +
    									" " + sixthString + " " + seventhString);
    							
    							String rowdata = firstString + " " + secondString + " " + thirdString + " " + fourthString + " " + fifthString + " " +
    							" " + sixthString + " " + seventhString;
    							
    							rowdatatotal = rowdatatotal + rowdata + "\n" ;
    							sqloutputArea.setText (rowdatatotal + "\n");
    							} //end while
    						*/
    						con.close();
    						 //end try
    could anyone point me in the right direction, I need to get this done by tomorrow :(

    Thanks in advance, I appreciate your time.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    You have to format the code when posting.There are plenty of sample code in net.
    Go thru this below link
    Populate data from database into JTable in Netbeans | My Java Articles
    Ramya:cool:

Similar Threads

  1. JDBC and mySQL database trouble
    By ichwar in forum JDBC
    Replies: 17
    Last Post: 08-16-2010, 11:21 PM
  2. JTables and Databases
    By sehudson in forum Advanced Java
    Replies: 8
    Last Post: 03-25-2010, 09:01 AM
  3. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 08:01 PM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. Scrolling with JTables
    By hiranya in forum AWT / Swing
    Replies: 5
    Last Post: 10-30-2007, 08:48 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
  •