• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

regarding JTable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to Populate a JTable with MySQL Data after clicking JButton

datails:
data is present in mysql database in tabular form and i want to fetch this data from the database in the form of JTable after clicking JButton
:
please help
 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get a ResultSet which has rows, each row with several columns in. You can get the details from the ResultSetMetaData object associated with the ResultSet, eg how many rows, name of column, etc.
You can get the contents of the ResultSet with its next() and getXXX() methods, put them into a List, or an Object[][], and populate the JTable.
Or you can extend DefaultTableModel.
Go through the Java Tutorials and see whether that helps.

And welcome to JavaRanch
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am new to java
i am preparing a project called supply chain system
i took help on it through tutorials on the internet
but i could not exactly understand what you have written
can you please explain it with the help of example
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) You could also use other methods, like getString for only string representations. Ideally you should use meta.getColumnType(i) for that. For instance, you can have the following mapping:

Check the rest of java.sql.Types for what it could return, and be creative in the mapping.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have tried it but it is not resulting that i wants
here is the coding of java file which is for opening contents present in mysql database in the form of JTable after clicking JButton
/*
*/
please help

[Edit]Sorry, but even though it is a private URL I think it's still wiser to remove the password. Rob[/Edit]
[ October 26, 2008: Message edited by: Rob Prime ]
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravic johnson:
i have tried it but it is not resulting that i wants



You will probably get better help if you go into greater detail here. What is this code doing that is wrong? What is it not doing that it should be doing? etc...
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravic johnson:
JPanel panel = new JPanel();
panel.add(new JScrollPane(table),BorderLayout.CENTER);
panel.setForeground(java.awt.Color.lightGray);


This is probably not the main problem, but it is still a problem.

JPanel has a FlowLayout by default, so your code will not put in the center of your panel and resize it when your user interface resizes. You need to explicitly give the JPanel a new BorderLayout() for that to work.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to your advice i went into greater details that is trying to understand the defaulttablemodel concept
i worked on the coding part
made some changes in the coding part
but again i could not to achieve what i want
i am putting that coding part here
please help
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is it doing what you don't want, or not doing what you want?

I've tried your code (just changed the database details of course) and it worked just fine.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to say to populate a jTable with mysql data after clicking a JButton
this coding i have given will run quit good without clicking a JButton
that is to run :java student
but i want to populate a JTable with mysql database data after clicking a Jbutton
here JButton implements a actionlistener on clicking a new interface should open
that i want to say
please help
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you know how to fill a DefaultTableModel with your data know. So all you need to do is put that part of the code in your action listener, and when it's done call table.setModel(tableModel).

You will notice that your program seems to be hanging while your table model is being filled. For the reason and a solution check JProgressBat Doesn't Update. In short, all the filling code should be put in a different thread, and when it's done the "table.setmodel(tableModel)" call should be on the Event Dispatcher Thread.

An easy way is using javax.swing.SwingWorker:

You could have the loading seem more dynamic using publish and process but let's keep it simple for now.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i am to able to know how to fill the defaulttablemodel with data
but i have tried putting that part of coding into java file that implements actionlistener but it yields errors
i am putting code here unchanged of that part includes JButton that after clicking i expects must proceed to show JTable with mysql data



can you explain it how to implement it with this coding
please help
[ October 29, 2008: Message edited by: ravic johnson ]
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravic johnson:
but it yields errors


What errors?
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in class Supplier which coding i previously posted that implements actionlistener if i put that portion how to fill defaulttablemodel with data with coding changed is as follows


when compiled it results following errors
C:\jdk\bin>javac Supplier.java
Supplier.java:99: <identifier> expected
tableModel.setRowCount(0);
^
Supplier.java:100: <identifier> expected
tableModel.setColumnCount(0);
^
Supplier.java:107: illegal start of type
for(int i=1; i<=columnCount; i++)
^
Supplier.java:112: <identifier> expected
Vector data = new Vector();
^
Supplier.java:116: illegal start of type
while(result.next())
^
Supplier.java:151: <identifier> expected
}


please help
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just before "TableModel model = new TableModel(data,columns);" you close your method, so the rest is just dangling inside the class definition. And inside the class definition you cannot have statements.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have put the lines of code inside the method that is under initComponents method but it even it results error
/*
C:\jdk\bin>javac Supplier.java
Supplier.java:95: cannot find symbol
symbol : class ResultSetMetaData
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:95: cannot find symbol
symbol : variable result
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:98: javax.swing.table.TableModel is abstract; cannot be instantiat
ed
TableModel model = new TableModel(data,columns);
^
Supplier.java:103: cannot find symbol
symbol : class ResultSetMetaData
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:103: cannot find symbol
symbol : variable result
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:104: columnCount is already defined in initComponents()
int columnCount = md.getColumnCount();
^
Supplier.java:106: columns is already defined in initComponents()
Vector columns = new Vector(columnCount);
^
Supplier.java:114: data is already defined in initComponents()
Vector data = new Vector();
^
Supplier.java:115: row is already defined in initComponents()
Vector row;
^
Supplier.java:118: cannot find symbol
symbol : variable result
location: class Supplier
while(result.next())
^
Supplier.java:121: row is already defined in initComponents()
Object[] row = new Object[columns];
^
Supplier.java:121: incompatible types
found : java.util.Vector
required: int
Object[] row = new Object[columns];
^
Supplier.java:123: cannot find symbol
symbol : variable result
location: class Supplier
{row[i - 1] = result.getObject(i);
^
13 errors
*/
coding of supplier is as follows

i again want to say that i want to populate a JTable with mysql data after clicking JButton
can you please explain what you have advised

Well, you know how to fill a DefaultTableModel with your data know. So all you need to do is put that part of the code in your action listener, and when it's done call table.setModel(tableModel).


i am quite new to swing and AWT concepts

please help i want to submit this project in the first week of November
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those errors are quite basic. You should start by importing the right classes. Also, you should create a new DefaultTableModel. TableModel itself is an interface.
 
ravic johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to ask what is the concept behind putting defaulttablemodel with data in the part (supplier java file) which implements action listener
please explain
reply
    Bookmark Topic Watch Topic
  • New Topic