0% found this document useful (0 votes)
2 views4 pages

Practical No 29

The document provides two Java JDBC programs: one for retrieving student data from a MySQL database and another for updating a student's record. The first program connects to the database, executes a SELECT query, and prints the results. The second program updates a student's details based on their ID using a prepared statement and confirms the success of the operation.

Uploaded by

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

Practical No 29

The document provides two Java JDBC programs: one for retrieving student data from a MySQL database and another for updating a student's record. The first program connects to the database, executes a SELECT query, and prints the results. The second program updates a student's details based on their ID using a prepared statement and confirms the success of the operation.

Uploaded by

Sairaj Sawake
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical No.

29

3. Develop JDBC program to retrieve data.

Code

package retrievedataexample;

import [Link];

import [Link];

import [Link];

import [Link];

public class RetrieveDataExample {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/Students";

String username = "root";

String password = "3367";

String selectQuery = "SELECT id, name, age, grade FROM students";

try {

Connection connection =
[Link]("jdbc:mysql://localhost:3306/Students", "root", "3367");

Statement statement = [Link]();

ResultSet resultSet = [Link](selectQuery);

[Link]("ID\tName\tAge\tGrade");

while ([Link]()) {

int id = [Link]("id");

String name = [Link]("name");

int age = [Link]("age");

String grade = [Link]("grade");

[Link](id + "\t" + name + "\t" + age + "\t" + grade);

}
[Link]();

[Link]();

[Link]();

} catch (Exception e) {

[Link]();

Output:-
4. Develop a program to update a record in database table

Code

package updaterecordexample;

import [Link];

import [Link];

import [Link];

public class UpdateRecordExample {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/Students";

String username = "root";

String password = "3367";

String updateQuery = "UPDATE students SET name = ?, age = ?, grade = ? WHERE id = ?";

try {

Connection connection =
[Link]("jdbc:mysql://localhost:3306/Students", "root", "3367");

PreparedStatement preparedStatement = [Link](updateQuery);

[Link](1, "John Doe");

[Link](2, 25);

[Link](3, "A");

[Link](4, 1);

int rowsUpdated = [Link]();

if (rowsUpdated > 0) {

[Link]("Record updated successfully!");

} else {

[Link]("No record found with the given ID.");

[Link]();

[Link]();
} catch (Exception e) {

[Link]();

Output:-

You might also like