This source code is all about updating data from your MySQL database using your Java application.
We use Netbeans as our IDE and it is tested in 2.0 version. This operation performs only to a specific row from your database. You cannot update the entire rows from your database because it is not the correct way to execute.
Most of the programs that used a database, the program can update only a specific row from the database using a unique key. But there some other executions that you can update the entire rows from your database.
This tutorial uses an algorithm to update a specific row from your MySQL database. Before anything else, make sure that you already perform the Save features. Save Data in MySQL Using Java Application
Source code below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
if (data1.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data1 is empty"); | |
}else if (data2.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data2 is empty"); | |
}else if (data3.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data3 is empty"); | |
}else{ | |
String str="UPDATE tablename SET d1='" + data1.getText + "',d2='" + data2.getText + "'d3='" + data3.getText + "' WHERE id='" + id + "'"; | |
pst=conn.prepareStatement(str); | |
pst.setString(1, type); | |
pst.setString(2, bn.getText()); | |
pst.setString(3, business.getText()); | |
pst.execute(); | |
JOptionPane.showMessageDialog(null,"Data Updated!"); | |
} | |
} | |
catch (Exception e){ | |
JOptionPane.showMessageDialog(this, e.getMessage()); | |
} |
The code above uses a Try-catch exception, this block of codes can catch all the errors trows from your statement.
We also use an if statement, this block of codes notify the user if some fields are empty.
0 Comments