Binding JTable with MySQL table

I got this inquiry recently. I decided to try it myself. Here, I am showing through screen shots how to bind database table to a JTable. I am using MySQL as that is what was requested. I wanted to highlight how easy it is to use NetBeans to do this task.  NetBeans takes care of all the persistence  part for you. In fact, I did not have to write any code at all for this.

Here are the steps:

Create a Java Application Project. From main menu File -> New Projects. Choose Java category, and  Java  Application.

 

Now delete the Main.java file under Source Packages. This file was automatically created by the wizard for you. Add a new JFrame Form . Right Click Project node -> New -> JFrame Form.

Drag and Drop JTable from the palette on to the designer, as shown below.

Now Drag and drop the database Table on to the JTable in the designer. To do this, go to the Services tab , and expand the Databases -> Your MySQL database node -> Table, and select the table you want to bind to the JTable. Drag and drop it on to the JTable on the designer.


Make sure the MySQL driver jar file is under the libraries node of the project. Build and Run the project. when running the project it will ask you to select the main class. This is because we deleted the main class earlier, and we now need to provide the main class to begin execution. Select the default as shown below.

Here's the screen shot on running the application. 


That's all there is to it. Not a line of code was written. If you browse in the projects window, you'll see the IDE automatically created the Person.java class and persistence.xml files.

Comments:

how can i configure the jtable for this example ?

Posted by joshua morotti on April 13, 2008 at 05:23 PM PDT #

I've tried the above, but I get a no entry symbol when I try to drag-and-drop a DB table onto a JTable. Perhaps there is some property of the table that I need to set?

Posted by David Etheridge on April 16, 2008 at 10:24 PM PDT #

Query q = em.createNativeQuery(

"SELECT o.id, o.quantity, o.item " +

"FROM Order o, Item i " +

"WHERE (o.item = i.id) AND (i.name = 'widget')",

com.acme.Order.class);

List orders = q.getResultList();

Posted by Steve Howerton on May 14, 2008 at 01:56 AM PDT #

I have the tutorial running with a entire database displayed.
I am working with the tutorial at:
http://www.netbeans.org/kb/articles/mysql.html

The above tutorial creates a jDesktopAPP. with components bound to the table.

I am trying to add a button (basically).

I am in the process of understanding binding.

the program loads with <list> which is the entire database.

I created a new query (createQuery())

I would like to clear that list and repoulate it with a getResultSet()

As far as binding goes I have never acessed propertiest to set them or accessed properties

to work with them.

Could some one give me some idea where to start?

I have coded in a combobox,textfield,button(@Action)and actionPerformed()
Right now everything compiles yet the query is not created.

Can someone tell me some basic procedures I must follow to utilize persistence?

All this code does is add the above mentioned components and I added an error message to

try to put my question into context.
Thank for your time.

Help with Code Tags
(Toggle Plain Text)

private void searchJComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {

setSearchItem((String) searchJComboBox1.getSelectedItem());
if (searchJComboBox1.getSelectedItem().equals("by Artist")) {
String holdQuery = searchJComboBox1.getSelectedItem().toString();
setSearchInput(holdQuery);

//searchJTextField1.setText(getSearchQuery());
}
if (searchJComboBox1.getSelectedItem().equals("by Song")) {
String holdQuery = searchJComboBox1.getSelectedItem().toString();
setSearchInput(holdQuery);

//searchJTextField1.setText(getSearchQuery());
}
}

private void searchJButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if (searchJComboBox1.getSelectedItem().equals("by Artist")) {
String like = searchJTextField1.getText();
setSearchQueryString("SELECT c FROM content c WHERE c.artist LIKE :like");
searchQuery1 = entityManager.createQuery(getSearchQueryString());
searchQuery1.setParameter("like", "%" + searchJTextField1.getText() + "%");
List<Content> List = searchQuery1.getResultList();
}
if (searchJComboBox1.getSelectedItem().equals("by Song")) {
String like = searchJTextField1.getText();
setSearchQueryString("SELECT c FROM content c WHERE c.song LIKE :like");
searchQuery1 = entityManager.createQuery(getSearchQueryString());
searchQuery1.setParameter("like", "%" + searchJTextField1.getText() + "%");
List<Content> List = searchQuery1.getResultList();
}
}

@Action
public Task searchSplash() {

return new SearchSplashTask(getApplication());
}

private class SearchSplashTask extends Task {

SearchSplashTask(org.jdesktop.application.Application app) {
super(app);
}

@SuppressWarnings("unchecked")
@Override
protected Void doInBackground() {
try {
setProgress(0, 0, 4);
setMessage("Rolling back the current changes...");
setProgress(1, 0, 4);
entityManager.getTransaction().rollback();
Thread.sleep(1000L); // remove for real app

setProgress(2, 0, 4);

setMessage("Starting a new transaction...");
entityManager.getTransaction().begin();
Thread.sleep(500L); // remove for real app

setProgress(3, 0, 4);

setMessage("Fetching new data...");
java.util.Collection data = searchQuery1.getResultList();

for (Object entity : data) {
entityManager.refresh(entity);
}

Thread.sleep(1300L); // remove for real app

setProgress(4, 0, 4);

Thread.sleep(150L); // remove for real app

list.clear();
//list.addAll(search);
} catch (InterruptedException ignore) {
}
return null;
}

@Override
protected void finished() {
setMessage("Done.");
setSaveNeeded(false);
}
}

private void searchJComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { setSearchItem((String) searchJComboBox1.getSelectedItem()); if (searchJComboBox1.getSelectedItem().equals("by Artist")) { String holdQuery = searchJComboBox1.getSelectedItem().toString(); setSearchInput(holdQuery); //searchJTextField1.setText(getSearchQuery()); } if (searchJComboBox1.getSelectedItem().equals("by Song")) { String holdQuery = searchJComboBox1.getSelectedItem().toString(); setSearchInput(holdQuery); //searchJTextField1.setText(getSearchQuery()); } } private void searchJButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (searchJComboBox1.getSelectedItem().equals("by Artist")) { String like = searchJTextField1.getText(); setSearchQueryString("SELECT c FROM content c WHERE c.artist LIKE :like"); searchQuery1 = entityManager.createQuery(getSearchQueryString()); searchQuery1.setParameter("like", "%" + searchJTextField1.getText() + "%"); List<Content> List = searchQuery1.getResultList(); } if (searchJComboBox1.getSelectedItem().equals("by Song")) { String like = searchJTextField1.getText(); setSearchQueryString("SELECT c FROM content c WHERE c.song LIKE :like"); searchQuery1 = entityManager.createQuery(getSearchQueryString()); searchQuery1.setParameter("like", "%" + searchJTextField1.getText() + "%"); List<Content> List = searchQuery1.getResultList(); } } @Action public Task searchSplash() { return new SearchSplashTask(getApplication()); } private class SearchSplashTask extends Task { SearchSplashTask(org.jdesktop.application.Application app) { super(app); } @SuppressWarnings("unchecked") @Override protected Void doInBackground() { try { setProgress(0, 0, 4); setMessage("Rolling back the current changes..."); setProgress(1, 0, 4); entityManager.getTransaction().rollback(); Thread.sleep(1000L); // remove for real app setProgress(2, 0, 4); setMessage("Starting a new transaction..."); entityManager.getTransaction().begin(); Thread.sleep(500L); // remove for real app setProgress(3, 0, 4); setMessage("Fetching new data..."); java.util.Collection data = searchQuery1.getResultList(); for (Object entity : data) { entityManager.refresh(entity); } Thread.sleep(1300L); // remove for real app setProgress(4, 0, 4); Thread.sleep(150L); // remove for real app list.clear(); //list.addAll(search); } catch (InterruptedException ignore) { } return null; } @Override protected void finished() { setMessage("Done."); setSaveNeeded(false); } }

Help with Code Tags
(Toggle Plain Text)

entityManager = java.beans.Beans.isDesignTime() ? null :

javax.persistence.Persistence.createEntityManagerFactory

("splashbookdbPU").createEntityManager();
org.jdesktop.application.ResourceMap resourceMap =

org.jdesktop.application.Application.getInstance

(splashtemplateda.SplashTemplateDAApp.class).getContext().getResourceMap

(SplashTemplateDAView.class);
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery

(resourceMap.getString("query.query")); // NOI18N
list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() :

org.jdesktop.observablecollections.ObservableCollections.observableList

(query.getResultList());
searchQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery

(null);

entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory ("splashbookdbPU").createEntityManager(); org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance (splashtemplateda.SplashTemplateDAApp.class).getContext().getResourceMap (SplashTemplateDAView.class); query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery (resourceMap.getString("query.query")); // NOI18N list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList (query.getResultList()); searchQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery (null);

Help with Code Tags
(Toggle Plain Text)

init:
deps-jar:
Copying 1 file to

C:\\Users\\depot\\Documents\\ceyesuma\\program_cache\\in_progress\\Enterprise\\splashbookinprogress

\\target\\SplashTemplateDA\\build\\classes
compile:
run:
[TopLink Info]: 2008.05.12 05:49:17.417--ServerSession(25094328)--TopLink, version: Oracle

TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))
[TopLink Info]: 2008.05.12 05:49:19.438--ServerSession(25094328)--

file:/C:/Users/depot/Documents/ceyesuma/program_cache/in_progress/Enterprise/splashbookinpr

ogress/target/SplashTemplateDA/build/classes/-splashbookdbPU login successful
May 12, 2008 5:49:20 PM org.jdesktop.application.Application$1 run
SEVERE: Application class splashtemplateda.SplashTemplateDAApp failed to launch
java.lang.IllegalArgumentException: An exception occured while creating a query in

EntityManager
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.createQuery

(EntityManagerImpl.java:209)
at splashtemplateda.SplashTemplateDAView.initComponents

(SplashTemplateDAView.java:360)
at splashtemplateda.SplashTemplateDAView.<init>(SplashTemplateDAView.java:42)
at splashtemplateda.SplashTemplateDAApp.startup(SplashTemplateDAApp.java:19)
at org.jdesktop.application.Application$1.run(Application.java:171)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters

(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy

(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: Exception [TOPLINK-8002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs

(12/06/2007))): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: General Problem parsing the query [null]. The parser returned the

following [null].
Internal Exception: java.lang.NullPointerException
at oracle.toplink.essentials.exceptions.EJBQLException.generalParsingException

(EJBQLException.java:140)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParserFor

(EJBQLParser.java:155)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParseTree

(EJBQLParser.java:126)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:215)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:189)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:153)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.<init>

(EJBQueryImpl.java:114)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.<init>

(EJBQueryImpl.java:99)
at oracle.toplink.essentials.internal.ejb.cmp3.EJBQueryImpl.<init>

(EJBQueryImpl.java:86)
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.createQuery

(EntityManagerImpl.java:204)
... 12 more
Caused by: java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:33)
at

oracle.toplink.essentials.internal.parsing.ejbql.antlr273.EJBQLParserBuilder.buildParser

(EJBQLParserBuilder.java:61)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParserFor

(EJBQLParser.java:149)
... 20 more
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class

splashtemplateda.SplashTemplateDAApp failed to launch
at org.jdesktop.application.Application$1.run(Application.java:177)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters

(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy

(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: java.lang.IllegalArgumentException: An exception occured while creating a query

in EntityManager
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.createQuery

(EntityManagerImpl.java:209)
at splashtemplateda.SplashTemplateDAView.initComponents

(SplashTemplateDAView.java:360)
at splashtemplateda.SplashTemplateDAView.<init>(SplashTemplateDAView.java:42)
at splashtemplateda.SplashTemplateDAApp.startup(SplashTemplateDAApp.java:19)
at org.jdesktop.application.Application$1.run(Application.java:171)
... 8 more
Caused by: Exception [TOPLINK-8002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs

(12/06/2007))): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: General Problem parsing the query [null]. The parser returned the

following [null].
Internal Exception: java.lang.NullPointerException
at oracle.toplink.essentials.exceptions.EJBQLException.generalParsingException

(EJBQLException.java:140)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParserFor

(EJBQLParser.java:155)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParseTree

(EJBQLParser.java:126)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:215)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:189)
at

oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery

(EJBQueryImpl.java:153)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.<init>

(EJBQueryImpl.java:114)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.<init>

(EJBQueryImpl.java:99)
at oracle.toplink.essentials.internal.ejb.cmp3.EJBQueryImpl.<init>

(EJBQueryImpl.java:86)
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.createQuery

(EntityManagerImpl.java:204)
... 12 more
Caused by: java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:33)
at

oracle.toplink.essentials.internal.parsing.ejbql.antlr273.EJBQLParserBuilder.buildParser

(EJBQLParserBuilder.java:61)
at oracle.toplink.essentials.internal.parsing.ejbql.EJBQLParser.buildParserFor

(EJBQLParser.java:149)
... 20 more
BUILD SUCCESSFUL (total time: 11 seconds)

Posted by Steve Howerton on May 14, 2008 at 01:58 AM PDT #

i get a strange error. After i have dragged and dropped my "bankdb" table onto the jtable, it seems to look fine, and in the design view of the GUI i am building, it shows the various columns of my table. The form even builds. But when i try to run it, it says there is no persistance file. But the persistance file is there! netbeans has created it!

Error message: Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: No Persistence provider for EntityManager named bankdbPU: No META-INF/persistence.xml was found in classpath.

Posted by s1ice on May 28, 2008 at 12:29 AM PDT #

Can you take a look at the following and see if they help?

http://blogs.sun.com/pblaha/entry/using_toplink_persistence_ri_in

http://forums.java.net/jive/thread.jspa?threadID=26058

Posted by Karthik on June 02, 2008 at 03:26 AM PDT #

I want to make a Jtable including jcombobox how do that in netBeans.

Posted by Palitha Rupasinghe on June 16, 2008 at 12:35 PM PDT #

Excellent explanation.
it helped me too much.

Posted by Tamal on November 26, 2008 at 01:49 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

A blog for NetBeans users maintained by NetBeans Support team. For professional support, please check out Support Resources.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today