Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners update a JTable

Have Programming Question? Ask it here!
 
 
 


tushar087
update a JTable
1 Answer(s)      5 years and 10 months ago
Posted in : Java Beginners



Advertisement
View Answers

November 13, 2008 at 3:39 AM


Hi friend,


Please implement like the follows code.


I am going to post the working code for displaying a MySQL table in an updatable JTable

You would create the table as follows:

conn = getConnection();
stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stat.executeQuery("SELECT * FROM register");
ResultSetTableModel model = new ResultSetTableModel(rs);
table = new JTable(model);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
scrollPane = new JScrollPane(table);
pane.add(scrollPane, BorderLayout.CENTER);
validate();

And here is the working table model I use:

class ResultSetTableModel extends AbstractTableModel {

public ResultSetTableModel(ResultSet aResultSet) {
rs = aResultSet;

try {
rsmd = rs.getMetaData();
}
catch(SQLException e) {
e.printStackTrace();
}
}


public String getColumnName(int c){
try{
return rsmd.getColumnName(c + 1);
}
catch(SQLException e){
e.printStackTrace();
return "";
}
}

public boolean isCellEditable(int row, int col){
return true;
}

public int getColumnCount() {
try{
return rsmd.getColumnCount();
}
catch (SQLException e){
e.printStackTrace();
return 0;
}
}

public Object getValueAt(int r, int c) {
try {
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
}

public void setValueAt(Object aValue, int rowIndex, int columnIndex){
try
{
rs.absolute(rowIndex + 1);
System.out.println("I got here... row: "+ rowIndex + ", " + columnIndex);
rs.updateString(columnIndex+1, (String)aValue);
rs.updateRow();
}
catch(SQLException e)
{
e.printStackTrace();
}
}

public int getRowCount() {
try {
rs.last();
return rs.getRow();
}
catch(SQLException e) {
e.printStackTrace();
return 0;
}
}

private ResultSetMetaData rsmd;
}
----------------------------------------------
Read for more information.

http://www.roseindia.net/java/example/java/swing/
Thanks.


Related Tutorials/Questions & Answers:
update a JTable - Java Beginners
update a JTable   how to update a JTable with Mysql data through user... in an updatable JTable You would create the table as follows: conn... = new ResultSetTableModel(rs); table = new JTable(model
update a JTable - Java Beginners
update a JTable   i have tried your advice as how to update a JTable... main(String args[]) { JTable table; Connection con = null..."); ResultSetTableModel model = new ResultSetTableModel(rs); table = new JTable(model
Advertisements
how update JTable after adding a row into database
how update JTable after adding a row into database  J have two... in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djile pak.java
how to make JTable to add delete and update sql database table
how to make JTable to add delete and update sql database table  Hello all I want to know how to make JTable to act actively to add delete and update database table. i am struck ed here from long time please help me
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete the database
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have to update the cell values from there only means that whatever values i ma entering
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... is shown in the jtable.. of my jframe window.Now as per my requirement i have to add ,update,delete database values from jtable only so i added three buttons add
How to delete and update from Jtable cell in swing app
How to delete and update from Jtable cell in swing app  Hii Sir, I am developing a swing app for adding,updating and deleting from jtable... is getting removed from the jtable but selected row is getting deleted from
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... in the jtable.. of my jframe window.Now as per my requirement i have to add ,update,delete database values from jtable only so i added three buttons add,update
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... in the jtable.. of my jframe window.Now as per my requirement i have to add ,update,delete database values from jtable only so i added three buttons add,update
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
update
update  how can i update multiple records in database using jsp ,servlet and jdbc based on selection of checkbox in jsp
update
update  Predict and justify the output of the following code snippet written by the developer to update the Status table: String str = "UPDATE m...://localhost:3306/roseindia", "root", "root"); String str = "UPDATE Status SET
jtable problem
jtable problem  how to make a cell text hypertext
JTABLE OF JAVA
JTABLE OF JAVA  i have a jtable in java,i have used checkbox in jtable. now i want to add(submit) only those records that i have checked by checkbox how? i want small example with coding
Jtable-Java
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
JTABLE Issue
JTABLE Issue  Hi Eveyone, I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search... on basis of input data provided in input field. For JTABLE is on some other
jtable query
jtable query  I need a syntax...where i could fetch the whole data from the database once i click the cell in jtable...and that must be displayed in the nearby text field which i have set in the same frame
java jtable
java jtable  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read only... that in jtable. plz help me with the code
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container... a container on the jtable. kindly help me sir. thanks in advance regards, rajahari
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
REPORT WITH JTABLE
(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
JTable - JDBC
JTable   Hello..... I have Jtable with four rows and columns and i have also entered some records in MsSql database. i want to increase Jtable's... { JFrame f; JPanel p; JLabel l; JTextField tf; JButton btn; JTable tb
update profile
update profile  coding for update profile
how to update
how to update   conditional update
update image
update image  sir, I want to do update image into database
JTable - Swing AWT
JTable row selection event   Hi, it will be great if someone can share an example of row selection event in JTable
JTable hold different components
JTable hold different components  create Jtable with two coloums,one coloumn is combobox and other normal
JTable in java - Java Beginners
JTable in java  Sir , I have created an application with a JTable showing the records of an MS Access database table. On the same frame I have... given one , JTable table; ....... .... table.print(); Here the error
JTable - Java Beginners
JTable  can we merge two cells of one row in jtable
restrict jtable editing
restrict jtable editing  How to restrict jtable from editing or JTable disable editing?   public class MyTableModel extends...){ return false; } }   Disabling User Edits in a JTable Component
update query
update query  using oops concept in php.. How to update the data from databse ? with program example
JTable - Java Beginners
JTable search example  I've problem about JTable, i wan to search records when i types words in JTable cell,can u give the source code.I'm Beginner and i start begins learning java programming.Please send the example into my
to update the information
to update the information   sir, i am working on library mgt project. front end is core java and backend is ms access.i want to open,update the information through form.please send me code earliar
Advertisements
 

 

 

DMCA.com