Ajp Exp 18 Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

X.

Program Code:
1. Write a Program to create a Student Table in database and insert a record in a Student table.

import java.sql.*;

public class ProgramCode_1 {


public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:4008/Student", "root", "root");
Statement st = con.createStatement()) {
st.execute("CREATE TABLE Student (Roll_No INT, Name VARCHAR(100), Percentage INT);");
System.out.println("Created Table");
st.execute("INSERT INTO Student (Roll_No, Name, Percentage) VALUES (1, 'Dhruwal Makwana', 90);");
st.execute("INSERT INTO Student (Roll_No, Name, Percentage) VALUES (2, 'Hrishikesh Parmar', 90);");
System.out.println("Inserted Record");
} catch (Exception e) {
e.printStackTrace();
}
}
}
XIII. Exercise :
1. Develop a program to create employee table in database having two columns “emp_id” and
“emp_name”.

import java.sql.*;

class CreateEmployeeTable {
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:4008/Student", "root", " root ");
Statement st = con.createStatement()) {
String createTableSQL = "CREATE TABLE employee (emp_id INT PRIMARY KEY, emp_name
VARCHAR(100));";
st.execute(createTableSQL);
System.out.println("Created Table 'employee'");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. Develop a program to display the name and roll_no of students from “student table” having
percentage > 70.

import java.sql.*;

class DisplayStudents {
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:4008/Student", "root", "root");
Statement st = con.createStatement()) {

String query = "SELECT Name, Roll_No FROM Student WHERE Percentage > 70;";
ResultSet rs = st.executeQuery(query);

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


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();
}
}

You might also like