By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

show data to jtable

P: 1
I have code like this, but still wrong.
how to show data from database in JTable.

Expand|Select|Wrap|Line Numbers
  1.  package my.JavaNetBean; 
  2. import java.sql.*;
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import javax.swing.table.*;
  12. import java.util.Vector;
  13.  
  14. public class SimpleTable extends JFrame {
  15. public SimpleTable() {
  16. super("Simple JTable Test");
  17. setSize(350, 200);
  18. setLocation(250,300);
  19. int ColCount;
  20. int i;
  21. int h;
  22. String[] headers;
  23.  
  24. try
  25. {
  26. Class.forName(
  27. "com.mysql.jdbc.Driver").newInstance() ;
  28. Connection conDB
  29. =DriverManager.getConnection( 
  30.  
  31. "jdbc:mysql://localhost/matx?user=root&password=");
  32.     ResultSet rset1 = null;
  33.     Statement stmt1 = null;
  34.     Statement stmt2 = null;
  35.     String ItemCode = null;
  36.     String Description = null;
  37.     String ItemGroup = null;
  38. stmt1 = conDB.createStatement( );
  39.  
  40.         rset1 = stmt1.executeQuery("select * from tmatx");
  41.  
  42. Vector cache;
  43. cache = new Vector();
  44. ResultSetMetaData meta =
  45. rset1.getMetaData();
  46. ColCount=meta.getColumnCount();
  47.  
  48. while (rset1.next())
  49. {
  50.  
  51. String [] record= new
  52. String[ColCount];
  53. for (i=0; i<ColCount; i++)
  54. {
  55.  
  56. record[i]=rset1.getString(i+1);
  57. }
  58.  
  59.          ItemCode = rset1.getString("ItemCode");    
  60.          Description = rset1.getString( "Description");
  61.          ItemGroup = rset1.getString( "ItemGroup");
  62.  
  63. JTable jt = new JTable(new String[][]{
  64. {ItemCode,Description}}, new String[]{"ItemCode","Description"});
  65.  
  66. JScrollPane jsp = new JScrollPane(jt);
  67. getContentPane().add(jsp,
  68. BorderLayout.CENTER);
  69. }
  70. catch (Exception e)
  71. {
  72.     System.out.println("failure to connect " + e);
  73.     return; //(exit(1));
  74. }
  75. public static void main(String args[]) {
  76. SimpleTable st = new SimpleTable();
  77. st.setVisible(true);
  78. }
  79. }
  80.  
May 11 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
I have code like this, but still wrong.
how to show data from database in JTable.

Expand|Select|Wrap|Line Numbers
  1.  package my.JavaNetBean; 
  2. import java.sql.*;
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import javax.swing.table.*;
  12. import java.util.Vector;
  13.  
  14. public class SimpleTable extends JFrame {
  15. public SimpleTable() {
  16. super("Simple JTable Test");
  17. setSize(350, 200);
  18. setLocation(250,300);
  19. int ColCount;
  20. int i;
  21. int h;
  22. String[] headers;
  23.  
  24. try
  25. {
  26. Class.forName(
  27. "com.mysql.jdbc.Driver").newInstance() ;
  28. Connection conDB
  29. =DriverManager.getConnection( 
  30.  
  31. "jdbc:mysql://localhost/matx?user=root&password=");
  32.     ResultSet rset1 = null;
  33.     Statement stmt1 = null;
  34.     Statement stmt2 = null;
  35.     String ItemCode = null;
  36.     String Description = null;
  37.     String ItemGroup = null;
  38. stmt1 = conDB.createStatement( );
  39.  
  40.         rset1 = stmt1.executeQuery("select * from tmatx");
  41.  
  42. Vector cache;
  43. cache = new Vector();
  44. ResultSetMetaData meta =
  45. rset1.getMetaData();
  46. ColCount=meta.getColumnCount();
  47.  
  48. while (rset1.next())
  49. {
  50.  
  51. String [] record= new
  52. String[ColCount];
  53. for (i=0; i<ColCount; i++)
  54. {
  55.  
  56. record[i]=rset1.getString(i+1);
  57. }
  58.  
  59.          ItemCode = rset1.getString("ItemCode");    
  60.          Description = rset1.getString( "Description");
  61.          ItemGroup = rset1.getString( "ItemGroup");
  62.  
  63. JTable jt = new JTable(new String[][]{
  64. {ItemCode,Description}}, new String[]{"ItemCode","Description"});
  65.  
  66. JScrollPane jsp = new JScrollPane(jt);
  67. getContentPane().add(jsp,
  68. BorderLayout.CENTER);
  69. }
  70. catch (Exception e)
  71. {
  72.     System.out.println("failure to connect " + e);
  73.     return; //(exit(1));
  74. }
  75. public static void main(String args[]) {
  76. SimpleTable st = new SimpleTable();
  77. st.setVisible(true);
  78. }
  79. }
  80.  
1.) Please use code tags everytime you post code
2.) Your code is more readable if you declare variables closest to where they are used.
3.) What error/problem are you getting with your code?
May 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.