Solved

How to update a database from a JTable?

Posted on 2009-04-08
2
1,012 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 52
The line on IDE 4 67
Java SE 8u111  Lot of stuff broke 11 53
even odd program using while loop 3 25
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now