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

Practical Java 2 7

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

Practical Java 2 7

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

Practical No.

7
1.Develop a program to create employee table in database
having two columns “product_id” and “product_name”.
Program:
import java.sql.*;
public class CreateEmployeeTable {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:1334/sakila", "root",
"root@shreyas");
Statement statement = conn.createStatement();
String sql = "CREATE TABLE employee (" +
"product_id INT AUTO_INCREMENT PRIMARY KEY," +
"product_name VARCHAR(255) NOT NULL)";
statement.executeUpdate(sql);
System.out.println("Table 'employee' created successfully!");
statement.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}}
Output:

2.Develop a program to display the name and roll_no of students


From “studentstable” having percentage>70.
import java.sql.*;
public class DisplayStudents {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:1334/sakila",
"root", "root@shreyas");
Statement statement = conn.createStatement();
String sql = "SELECT name, roll_no FROM studenttable WHERE
percentage > 70";
ResultSet resultSet = statement.executeQuery(sql);
System.out.println("Students with percentage > 70:");
System.out.println("Name\t\tRoll No");
System.out.println("---------------------------");
while (resultSet.next()) {
String name = resultSet.getString("name");
int rollNo = resultSet.getInt("roll_no");
System.out.println(name + "\t\t" + rollNo);
}
resultSet.close();
statement.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Output:
3.Develop a program to update name of a student from Rama to
Rohan.
import java.sql.*;
public class UpdateStudentName {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:1334/sakila", "root", "root@shreyas");
String sql = "UPDATE studenttable SET name = ? WHERE name = ?";
PreparedStatement preparedStatement =
conn.prepareStatement(sql);
preparedStatement.setString(1, "Rohan");
preparedStatement.setString(2, "Rama");
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Name changed from Rama to Rohan");
} else {
System.out.println("No records found with name Rama");
}
preparedStatement.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace(); } }}
Output:

4.Develop a program to delete all record for a product whose “price


is greater than 1000” and Id is “P1234”.
import java.sql.*;
public class DeleteProductRecords {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila",
"root", "root@shreyas");
String sql = "DELETE FROM product WHERE product_id = ?
AND price > ?";
PreparedStatement preparedStatement =
conn.prepareStatement(sql);
preparedStatement.setString(1, "P1234");
preparedStatement.setDouble(2, 1000.0);
int rowsAffected = preparedStatement.executeUpdate();
System.out.println("Record deleted successfully");
preparedStatement.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:

You might also like