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
Solved

How to update a database from a JTable?

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 38
add projects t working set in maven 2 23
Java program running SQL query 5 37
Tagging and Merging on Branch 1 30
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…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

860 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