Results 1 to 2 of 2
- 04-20-2010, 12:35 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
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)
could anyone point me in the right direction, I need to get this done by tomorrow :(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
Thanks in advance, I appreciate your time.
- 04-20-2010, 03:05 PM #2
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 ArticlesRamya:cool:
Similar Threads
-
JDBC and mySQL database trouble
By ichwar in forum JDBCReplies: 17Last Post: 08-16-2010, 11:21 PM -
JTables and Databases
By sehudson in forum Advanced JavaReplies: 8Last Post: 03-25-2010, 09:01 AM -
CheckBoxes and JTables
By lakshayghai in forum AWT / SwingReplies: 1Last Post: 03-16-2010, 08:01 PM -
How to convert access database to mysql database?
By vrk in forum Advanced JavaReplies: 2Last Post: 02-11-2009, 04:43 AM -
Scrolling with JTables
By hiranya in forum AWT / SwingReplies: 5Last Post: 10-30-2007, 08:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks