Solved

How to update a database from a JTable?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
parentbit challenge 3 52
Python Assistance 7 36
Groovy:unable to resolve class error 2 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 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

23 Experts available now in Live!

Get 1:1 Help Now