Solved

How to update a database from a JTable?

Posted on 2009-04-08
2
1,030 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

734 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