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

PR 18 Exercise

This Java program connects to a MySQL database and queries the student1 table to retrieve and display student records that have a percentage greater than 70. It loads the MySQL driver, establishes a database connection, creates statements to execute queries, runs a query to select relevant student records from the student1 table, iterates through the result set to display matching records, and closes the connection.

Uploaded by

Abc
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)
42 views4 pages

PR 18 Exercise

This Java program connects to a MySQL database and queries the student1 table to retrieve and display student records that have a percentage greater than 70. It loads the MySQL driver, establishes a database connection, creates statements to execute queries, runs a query to select relevant student records from the student1 table, iterates through the result set to display matching records, and closes the connection.

Uploaded by

Abc
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
You are on page 1/ 4

PR 18 EXERCISE

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class StudentDb { public static void main(String args[]) {

try { Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("Driver Loaded");

Connection con=DriverManager.getConnection("jdbc:mysql:///MSBTE", "root",


"Shriyash#3005");

System.out.println("connected"); Statement s=con.createStatement();

Statement si = con.createStatement(); Statement st=con.createStatement();

String s1; s1="select * from student1 where Percentage > 70";

ResultSet rs=st.executeQuery(s1);

rs=st.getResultSet();

System.out.println("Students having percentage > 70 :");

while(rs.next())

System.out.println(rs.getString("Name")+" "+rs.getInt("Rollno")+" "+rs.getInt("Percentage"));

con.close(); } catch(Exception e) { System.out.println(e); } } }

OUTPUT:
import java.sql.*;

public class pr18 {

public static void main(String[] args) {

try {

Connection c;

Class.forName( className: "com.mysql.cj.jdbc.Driver"); c = DriverManager.getConnection( url:


"jdbc:mysql:///MSBTE", user: "root", password: "Shriyash#3005");

Statement st = c.createStatement();

String str = "CREATE TABLE employee (emp_id INT, emp_name VARCHAR(20));";

st.execute(str);

st.close();

c.close();

} catch (Exception e) {

e.printStackTrace();

}
import java.sql.*;

class jdbcDemo{

public static void main(String[] args) {

try {

Connection c;

Class.forName("com.mysql.cj.jdbc.Driver");

System.out.println("Driver Loaded");

c = DriverManager.getConnection("jdbc:mysql:///MSBTE", "root", "Shriyash#3005");

System.out.println("connection to the database created");

Statement st = c.createStatement();

String str = "select * from student";

ResultSet rs = st.executeQuery(str);

String text = " ";

System.out.println("Roll Number \t Name");

while(rs.next()){

text = text+rs.getInt(1)+"\t"+rs.getString(2)+"\n";

System.out.println(text);

st.close();

c.close();

} catch (Exception e) {

e.printStackTrace();

}
}

You might also like