0% found this document useful (0 votes)
1 views

PracticalNo18_3

The document describes a practical exercise for inserting and retrieving data from a database using JDBC. It includes a Java program that connects to a MySQL database, retrieves student names and roll numbers for those with a percentage greater than 70, and prints the results. The program handles SQL exceptions and prints error messages if any occur.

Uploaded by

Ashish Mavani
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)
1 views

PracticalNo18_3

The document describes a practical exercise for inserting and retrieving data from a database using JDBC. It includes a Java program that connects to a MySQL database, retrieves student names and roll numbers for those with a percentage greater than 70, and prints the results. The program handles SQL exceptions and prints error messages if any occur.

Uploaded by

Ashish Mavani
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/ 1

Practical No.

: 18

Practical Name: Write a program to insert and retrieve data from database using JDBC.

Roll No.: 47

Student Name: Mavani Ashish Shantilal

Program:
import java.sql.*;

public class practical18_3 {


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student_DB";
String user = "root";
String password = "";

try (Connection conn = DriverManager.getConnection(url, user, password);


Statement stmt = conn.createStatement()) {

String selectSQL = "SELECT name, roll_no FROM Student WHERE percentage > 70";
ResultSet rs = stmt.executeQuery(selectSQL);

while (rs.next()) {
String name = rs.getString("name");
int rollNo = rs.getInt("roll_no");
System.out.println("Name: " + name + ", Roll No: " + rollNo);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
}
}

Output:

You might also like