Solved

How to update a database from a JTable?

Posted on 2009-04-08
2
1,017 Views
Last Modified: 2013-11-23
I have a form with a Jtable and need to update a row in it and update the row back to the database, The JTable is editable, I can change cell values but i don't know how to update the DB
0
Comment
Question by:imarquez
2 Comments
 
LVL 8

Accepted Solution

by:
Haris V earned 500 total points
ID: 24097401
You must do that manually.
If you come from m$ camp you probably are searching data binding. You can find it as far as i know for example in JBuilder(www.borland.com). But i prefer to do some data access layer, for example using hibernate(www.hibernate.org)
/**
 Some simple scenario
*/
setValueAt(Object o,int row,int col)
{
 String sql= "UPDATE Disease SET "+this.getColumnName(int col)+" = '"+(String o)+"' WHERE someId = "+this.getColumnName(0);//this.getColumnName(0) - your id column

doYourUpdateSql(sql);
}

Object getValueAt(int row, int col)
{
 String sql="SELECT "+this.getColumnName(col)+" FROM Disease WHERE someId="+this.getColumnName(0);
 String result=doYourQuery(sql);
 return result;
}



Lots of JTable samples here - http://www.javaalmanac.com/cgi-bin/search/find.pl?words=JTable

Make sure you close your connections once you've retrieved the values or updated them into the DB

0
 

Author Comment

by:imarquez
ID: 24097474
I already have a persistence layer using JPA, the model of the table gets its data from it, but I need to know how to do it "backwards", the model gets the updated row from the table and calls for the persistence layer to update that object (row).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 44
eclipse argument 14 60
Error trying to install RTMT Win7 5 60
maven module vs maven project 3 23
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question