In my previous JTable Video Tutorial I showed you how to pull information from a database and work with it in a JTable.
In this tutorial I show you how to add and delete rows in a JTable that will then effect the database. I also cover allowing the user to change the database on a cell basis.
After this tutorial, you’ll be able to do anything with Java JTables.
If you like videos like this, tell Google [googleplusone]
Also, feel free to share it [nttfblike]
Code from the Video
Hi Derek,
great job, those Java/Swing videos! You are almost at the level of Paul Hagerty’s lectures on iOS that is available on iTunes U.
One topic that I was looking for and couldn’t find so far: The overall structure of a MVC based swing application. What components take over what duties, what are the typical interfaces, visibility between the components etc. Wouldn’t that be a nice topic for you? Our can you highlight me a resource?
Best regards,
— Till.
I’m going to cover most of the most popular design patterns very soon. You can expect that tutorial to start next week. I’m sure the Stanford tutorials must be good. I don’t watch anyone else though, because I don’t want to be influenced by them.
Very soon I’ll completely change the way I do tutorials. I want them to be more fun. I figure that is the only way I can improve.
More fun is always good! Although I must admit your tutorials are the best quality I have seen so far: seeing you typing the things keeps the mental flow in sync, the production quality and layout are really professional!
What could help to explain more complex and high level topics are documents (mainly graphical models) beside the code. This way one could even understand MVC π
Thank you very much. I’m not sure how I can improve them. Not that I think they are perfect, but I’ve been perplexed by why I don’t get more views? I guess I spend very little time on marketing / networking and that is the reason. This is all just a fun hobby and definitely not even a part time job.
The design pattern tutorial is coming π
hiii, nice video as usual, thank you very much…
if possible make some videos for logging in java..
also with log4j..
thank you..
I’ll see what I can do. I was never a big fan of logging because it really junks up the code. I’ll see if I can find some great reasons to use this. Thanks for the request π
Hi,
Thanks for making such a wonderful website. You are the man who is really helping student like me.
From my very early age I am planning to make a desktop application for super shop management. But I am not getting proper guideline to make my dream successful. I have some knowledge in java. Would you please give me guideline about how to get started and what I need to learn , from where I could learn etc .
Thanks
You’re very welcome π I’m not sure what Super Shop Management is? If you can give me an example or explain what you want to do further, I’ll be happy to help
Hi Derek,
Thanks for sharing your knowledge.
I am knew to the world of java programming and find your videos very helpful.
If you can I would really like to see a set that shows how to pull information
from a database and work with it in a JList and on item selected open a detail
form to update and delete a field in the database.
Hi Joseph,
You are very welcome. I’m glad to help. I pretty much covered Java Jlists here. I then show how to query databases with Java here.
There is more information on Java and MySQL here. In this tutorial JTables and MySQL, I show how to delete information from the database using Swing components.
If I didn’t cover what you need feel free to ask. I’m going to revisit JTables, MySQL and sabermetrics soon. I’m going to make an app that has multiple JTables that work together.
Derek
Yes Derek I’ve been playing around with your base code and can now put things together. Thanks.
Hi admin,
Deleting a row doent work for me, seems like a cursor probleme:
java.sql.SQLException: [Microsoft][Gestionnaire de pilotes ODBC] Γtat de curseur non valide
at sun.jdbc.odbc.JdbcOdbcResultSet.setPos(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.deleteRow(Unknown Source)
Just like if the programe doesn’t know which row to delete π
What version of Java are you using?
1.6
Thanks.
You’re welcome
Very Nice video. I follow your code.. it works well without any errors. But there is a little thing about deleting rows.
when I select a row and press “remove” button.
In the swing interface the selected row is deleted (as expected).
In the actual database the last row is deleted whatever the selected row was (not expected).
This is the code for remove button:
JButton removeEmployee = new JButton("Remove Selected");
removeEmployee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dTableModel.removeRow(table.getSelectedRow());
try
{
resultRows.absolute(table.getSelectedRow());
resultRows.deleteRow();
} catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
Admin.. I am sorry about the previous comment, I didn’t know that I should be logged in to see it.
Ok here is the solution to my problem.
JButton removeEmployee = new JButton("Remove Selected");
removeEmployee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
resultSet.absolute(table.getSelectedRow()+1);
resultSet.deleteRow();
dTableModel.removeRow(table.getSelectedRow());
} catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
I put it and solved it in
The link where I put and solved it is here
http://stackoverflow.com/questions/12104882/java-delete-a-selected-row-resultset-deletes-last-row-from-the-database-instea
Thank you very much for pointing this out and finding an answer π I’m normally better at answering comments but my wife is pregnant and she is due today π Thanks again – Derek
Thank you! Same issue for me, now fixed
Hi Derek.
(By Derek):
>Iβm going to make an app that has multiple JTables that work >together.
It will be nice if you can cover that.
When I press add to the SLQ database I get this error:
Invalid cursor state – no current row.
Do you know what the problem is?
I solved the problem with direct SQL statement. But you code for the add button doesnt work for me. Not sure why. I think it looks to initialize the cursor position.
I’ll have to look into that. Did you change the code in any way? I’m glad you figured it out
dear Derek,
I have a question about synchronizing between the database and Jtable, I been playing around this example.
the Id number would always skip over the expected sequent order when I entered a new record by adding a record.
e.g,if there are only 2 records in the database, the new inserted row’s ID should be 3, it turned out to be 18 or other number instead, depending on how many operations I had done in between. it seems that java could memorize the number of time that i had added or removed the record. I couldn’t figure it out how to fix it. what is the general approach to synchronise with database?
Here is a page I linked to a while back when I was doing just that. It worked for me. I hope it helps
thank you so much for this video… its been three days m searching for this kinda program but nothing worked for me.. thanks for this program…. π
thanks for a wonderful video.. i have been searching for this kinda topic for past three days.. but in vain.. thank u so much this helped me alot..:)
You’re very welcome π I’m glad it helped
@derek.. i want to display a jtable that is used for fetching data from database , when i click on a row it should open a new frame with that particular information that has been clicked.. help me out!!!!!! π
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Do Your Stuff Here
}
});
i have followed the tutorial bu have been unable to store the records to the database. My connection is in another class
public class DatabaseConnection {public static Connection getDBConnection() {
Connection connection;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounting","root","jinXED007");
return connection;
} catch (Exception ex) {
return null;
}
}
What errors are you getting?
Hi Derek, I’ve been trying your code and I can add/remove information but when I try to change the cells the only cell that I’m available the edit and see in the database is “state”.
When I try to edit other cells nothing happeds :/ I’m using java version 7.0
Did you see part 37 and set up the database? Sorry I’m not sure aside from that
using the JTextField
1)how can we check for String length error, for example entering SSN length Not greater then 7
2)how to empty the JTextField after adding a row
3)if we add a new Row how to make that row permanent in JTable
thank you
I cover all of that over the course of my Java tutorial. You could catch the number of numbers entered by checking length in a keylistener. I have tutorials on JTextField methods and such as well. Here is every Java tutorial in one place Java Video Tutorial. I hope it helps π
i meant SSN length not greater 9 sorry
Yes an SSN is 9 numbers in length in the US