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

Prac 18

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)
24 views

Prac 18

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/ 4

1.

Write a Program to create a Student Table in database and insert a record in a Student
table.
Code:
import java.sql.*;
public class Prac_18_1 {
public Prac_18_1() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Pratik", "root", "");

Statement stmt = con.createStatement();

String table = "create table Student (" +


"name varchar(255), " +
"age int, " +
"grade varchar(10))";

stmt.executeUpdate(table);
System.out.println("Student table created successfully.");

String insert = "insert into Student (name, age, grade) values ('Pratik', 19, 'A')";

stmt.executeUpdate(insert);
System.out.println("Record inserted successfully.");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Prac_18_1();
}
}
Output:
2. Develop a program to create employee table in database having two columns “emp_id”
and “emp_name”
Code:
import java.sql.*;
public class Prac_18_2 {
public Prac_18_2() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Emp",


"root", "");

Statement stmt = con.createStatement();


String table = "create table Employee (" +
"emp_id INT, " +
"emp_name VARCHAR(255))";

stmt.executeUpdate(table);
System.out.println("Employee table created successfully.");

con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Prac_18_2();
}
}

Output:
3. Develop a program to display the name and roll_no of students from “student table”
having percentage > 70
Code:
import java.sql.*;
public class Prac_18_3 {
public Prac_18_3() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Pratik", "root", "");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT name, roll_no FROM student WHERE


percentage > 70");

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


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

public static void main(String[] args) {


new Prac_18_3();
}
}
Output:

You might also like