Student - SQL: - Go To Mysql Database and Click On Import Tab Click On Browse Button and Select This File (Student - SQL)
Student - SQL: - Go To Mysql Database and Click On Import Tab Click On Browse Button and Select This File (Student - SQL)
Student - SQL: - Go To Mysql Database and Click On Import Tab Click On Browse Button and Select This File (Student - SQL)
MySQL is an RDBMS (Relational Database Management System) database. In this
blog, we will learn using JDBC (Java Database Connectivity) to insert, update and
delete data into MySQL database using the NetBeans IDE.
student.sql
1. --Go to MySql database and click on Import tab click on Browse
button and select this file(student.sql)
2. create database Studentinformation;
3. use Studentinformation;
4. create table student(roll int(3) primary key, name varchar(20)
, marks int(3));
STEP 1
Open the NetBeans IDE. Click on File -> New Project, select the Java category;
from Projects, select Java Application. Click on the "Next" button.
STEP 2
Enter your project name and uncheck "Create Main Class" then click the "Finish"
button.
STEP 3
Expand your project folder and right-click on the Libraries folder and click "Add
Library…".
Select the "JDBC MYSQL Driver" click on the "Add Library" button.
STEP 4
Right-click on the project folder and go to New -> JFrame Form and enter your
Class Name. Now, click the "Finish" button.
STEP 5
Drag and drop three jLabel, three JTextField, and three button fields.
Now, change the jLabel text and the button text like in the below image.
STEP 6
Click on the "Source" button just beside the "Design" button.
1. // Import these package
2. import java.sql.Connection;
3. import java.sql.DriverManager;
4. import java.sql.SQLException;
5. import java.sql.Statement;
6. import javax.swing.JOptionPane;
7. // Create Referesh Method
8. public void Referesh() {
9. jTextField1.setText("");
10. jTextField2.setText("");
11. jTextField3.setText("");
12. }
Insert
Right-click on the "Insert" button. Go to Event -> action -> actionPerformed and
add the below code.
1. private void jButton1ActionPerformed(java.awt.event.ActionEven
t evt) {
2. try {
3. Class.forName("com.mysql.jdbc.Driver");
4. // establish connection
5. Connection con = DriverManager.getConnection("jdbc:mys
ql://localhost/Studentinformation", "root", "");
6. Statement statement = con.createStatement();
7. statement.executeUpdate("INSERT INTO student VALUES("
+ jTextField1.getText() + ",'" + jTextField2.getText() + "',"
+ jTextField3.getText() + ")");
8. JOptionPane.showMessageDialog(null, "Record inserted..
.");
9. statement.close();
10. con.close();
11. Referesh(); //Calling Referesh() method
12. } catch (SQLException | ClassNotFoundException e) {
13. JOptionPane.showMessageDialog(null, e);
14. }
15. }
Update
Right-click the "Update" button. Go to Event -> action -> actionPerformed and
add the below code.
1. private void jButton2ActionPerformed(java.awt.event.ActionEven
t evt) {
2. try {
3. Class.forName("com.mysql.jdbc.Driver");
4. // establish connection
5. Connection con = DriverManager.getConnection("jdbc:mys
ql://localhost/Studentinformation", "root", "");
6. Statement stmt = con.createStatement();
7. stmt.execute("UPDATE student SET name='" + jTextField2
.getText() + "',marks=" + jTextField3.getText() + " WHERE roll
=" + jTextField1.getText() + "");
8. JOptionPane.showMessageDialog(null, "Record is updated
...");
9. stmt.close();
10. con.close();
11. Referesh(); //Calling Referesh() method
12. } catch (SQLException | ClassNotFoundException se) {
13. JOptionPane.showMessageDialog(null, se);
14. }
15. }
Delete
Right-click the "Delete" button. Go to Event -> action -> actionPerformed and add
the below code.
1. private void jButton3ActionPerformed(java.awt.event.ActionEven
t evt) {
2. try {
3. Class.forName("com.mysql.jdbc.Driver");
4. // establish connection
5. Connection con = DriverManager.getConnection("jdbc:mys
ql://localhost/Studentinformation", "root", "");
6. Statement statement = con.createStatement();
7. statement.executeUpdate("DELETE FROM student WHERE rol
l=" + jTextField1.getText() + "");
8. JOptionPane.showMessageDialog(null, "Record deleted...
");
9. statement.close();
10. con.close();
11. Referesh(); //Calling Referesh() method
12. } catch (SQLException | ClassNotFoundException e) {
13. JOptionPane.showMessageDialog(null, e);
14. }
15. }
STEP 6
Now, your project is completed. Run (F6) the project.
In this blog, I covered MySQL database operations (Insert, Update and Delete)
with JDBC using NetBeans IDE. If you have any problem, please comment.