0% found this document useful (0 votes)
5 views2 pages

Newpdf 12

The document provides a Java example of using JDBC to insert a record into a MySQL database. It includes the necessary code to establish a connection, prepare an SQL insert statement, set parameters, and execute the insert. The output shows the updated records in the 'students' table after the insertion.

Uploaded by

shadaf8010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Newpdf 12

The document provides a Java example of using JDBC to insert a record into a MySQL database. It includes the necessary code to establish a connection, prepare an SQL insert statement, set parameters, and execute the insert. The output shows the updated records in the 'students' table after the insertion.

Uploaded by

shadaf8010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICAL NO.

:12
AIM:Implementation of JDBC to insert record in database.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcInsertExample {


// Database URL, username, and password\
private static nal String DB_URL = “jdbc:mysql://localhost:3306/Ruchika_db";
private static nal String USER = "root";
private static nal String PASS = "Rucha@123";

public static void main(String[] args) {


// SQL insert statement\
String insertSQL = "INSERT INTO students (StudentId, FirstName,LastName,Age) VALUES
(?, ?,?,?)";

// Establish connection and execute insert\


try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {

// Set parameters\
preparedStatement.setString(1, "4");
preparedStatement.setString(2, "Madhura");
preparedStatement.setString(3, "Jamkar");
preparedStatement.setString(4, "23");

// Execute the insert\


int rowsA ected = preparedStatement.executeUpdate();
System.out.println("Rows a ected: " + rowsA ected);

} catch (SQLException e) {
e.printStackTrace();
}
}
}

OUTPUT:

mysql> use Ruchika_db


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from students;
+-----------+-----------+----------+------+
| StudentID | FirstName | LastName | Age |
+-----------+-----------+----------+------+
| 1 | Ruchika | Ingale | 20 |
| 2 | Teju | Joshi | 22 |
| 3 | Aachal | Mohod | 21 |
| 4 | Madhura | Jamkar | 23 |
+-----------+-----------+----------+------+
3 rows in set (0.00 sec)
fi
fi
fi
ff
ff
ff

You might also like