Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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