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

Practical 18 A JP

The program code creates a Student table in a database with columns RollNo, Name, and Percentage. It then inserts 5 records into the Student table. Finally, it prints a message confirming the table was created and records inserted successfully. The output displays the roll number and name of students retrieved from the Student table whose percentage is greater than 70.

Uploaded by

Akanksha
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)
50 views

Practical 18 A JP

The program code creates a Student table in a database with columns RollNo, Name, and Percentage. It then inserts 5 records into the Student table. Finally, it prints a message confirming the table was created and records inserted successfully. The output displays the roll number and name of students retrieved from the Student table whose percentage is greater than 70.

Uploaded by

Akanksha
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/ 6

Program Code

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


Student table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentTableCreation {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:4306/Student";
String dbUsername = "root";
String dbPassword = "";
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection(jdbcUrl, dbUsername,
dbPassword);
statement = connection.createStatement();
// Create the Student table
String createTableSQL = "CREATE TABLE IF NOT EXISTS Student (" +
"RollNo INT PRIMARY KEY," +
"Name VARCHAR(50)," +
"Percentage DECIMAL(5, 2)" +
")";
statement.executeUpdate(createTableSQL);
// Insert 5 records into the Student table
String[] insertSQLs = {
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (33,
'Kartik', 80.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (23,
'Samarth', 82.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (28,
'Yash', 77.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (10,
'Anuj', 69.00)",
"INSERT INTO Student (RollNo, Name, Percentage) VALUES (11,
'Jeet', 65.00)"
};
for (String insertSQL : insertSQLs) {
statement.execute(insertSQL);
}
System.out.println("Student table created and records inserted
successfully.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output:

2. Write the output of following code.

import java.sql.*;
public class JdbcDemo {
public static void main(String[] args) {
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Define the connection URL
String url = "jdbc:mysql://localhost:4306/Student";
String username = "root";
String password = "";
// Establish a connection to the database
Connection cn = DriverManager.getConnection(url, username,
password);
System.out.println("Connection to the database created");
// Create a SQL statement
Statement st = cn.createStatement();
String str = "SELECT * FROM student";
// Execute the SQL query
ResultSet rs = st.executeQuery(str);
System.out.println("Roll Number \t Name");
// Process the results
while (rs.next()) {
int rollNumber = rs.getInt("RollNo");
String name = rs.getString("Name");
System.out.println(rollNumber + "\t" + name);
}
// Close the resources
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:
Exercise
1. Develop a program to create employee table in database having two columns
“emp_id” and “emp_name”.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class CreateEmployeeTable {


public static void main(String[] args) {
Connection connection = null;
Statement statement = null;

try {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:4306/Student";
String user = "root";
String password = "";

// Create a connection to the database


connection = DriverManager.getConnection(url, user, password);

// Create a SQL statement


statement = connection.createStatement();

// Define the SQL statement to create the "employee" table


String createTableSQL = "CREATE TABLE employee (" +
"emp_id INT AUTO_INCREMENT PRIMARY KEY, " +
"emp_name VARCHAR(255))";

// Execute the SQL statement to create the table


statement.executeUpdate(createTableSQL);

String[] insertSQLs = {
"INSERT INTO employee (emp_name) VALUES ( 'Kartik' )",
"INSERT INTO employee ( emp_name) VALUES ( 'Samarth')",
"INSERT INTO employee ( emp_name) VALUES ( 'Yash')",
"INSERT INTO employee ( emp_name) VALUES ( 'Anuj')",
"INSERT INTO employee ( emp_name) VALUES ( 'Jeet')"
};

for (String insertSQL : insertSQLs) {


statement.executeUpdate(insertSQL);
}

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


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Output:

2. Develop a program to display the name and roll_no of students from “student
table” having percentage > 70.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DisplayStudentsWithPercentage {


public static void main(String[] args) {
Connection connection = null;
Statement statement = null;

try {
// JDBC URL, username, and password of your MySQL server
String url = "jdbc:mysql://localhost:4306/Student";
String user = "root";
String password = "";

// Create a connection to the database


connection = DriverManager.getConnection(url, user, password);

// Create a SQL statement


statement = connection.createStatement();

// Define the SQL query to retrieve students with percentage > 70


String query = "SELECT RollNo, Name FROM Student WHERE percentage
> 70";

// Execute the SQL query


ResultSet resultSet = statement.executeQuery(query);

// Display the results


System.out.println("Roll No\tName");
while (resultSet.next()) {
int rollNo = resultSet.getInt("RollNo");
String name = resultSet.getString("Name");
System.out.println(rollNo + "\t" + name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Output:

You might also like