0% found this document useful (0 votes)
23 views4 pages

JDBC

This Java program uses JDBC to connect to a database and perform CRUD operations on a MOVIES table. It creates the table, inserts sample records, updates a record's genre field, deletes a record by id, and displays the remaining records.

Uploaded by

dinkeshjainjee
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)
23 views4 pages

JDBC

This Java program uses JDBC to connect to a database and perform CRUD operations on a MOVIES table. It creates the table, inserts sample records, updates a record's genre field, deletes a record by id, and displays the remaining records.

Uploaded by

dinkeshjainjee
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

13.

Write a Java program to display the employee id, age, first name and last name
using JDBC connectivity.
Test Data: ( In Database )
ID: 100, Age: 23, First: Raj, Last: SharmaID: 101, Age: 24, First: Bala, Last:
SinghID: 102, Age: 25, First: Anu, Last: PriyaID: 103, Age: 26, First: Riya,
Last: Khan
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class b {
public static void main(String[] args) {
try {
// Establish database connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe", "system",
"system");
// Create SQL statement
Statement stmt = con.createStatement();

// Execute SQL query


ResultSet rs = stmt.executeQuery("SELECT id, age, first_name,
last_name FROM employees");

// Display results
System.out.println("Employee Information:");
while (rs.next()) {
int employeeId = rs.getInt("id");
int age = rs.getInt("age");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");

System.out.println("Employee ID: " + employeeId + ", Age: " +


age + ", First Name: " + firstName + ", Last Name: " + lastName);
}

// Close resources
rs.close();
stmt.close();
con.close();

} catch (SQLException e) {
e.printStackTrace();
}
}
}

14. Using the JDBC API and any relational database make the following
queries:
create a table MOVIES with columns: id of type INTEGER AUTO
INCREMENT,title of type VARCHAR (255), genre of type VARCHAR
(255),yearOfRelease of type INTEGER. Note that a table named
MOVIE may already exist. In that case, delete it.
 add any three records to the MOVIES table
 update one selected record (use the PreparedStatement)
 delete selected record with specified id
 display all other records in the database
 import java.sql.*;

 public class a {
 public static void main(String[] args){
 try{
 Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521
/xe","system","system");

 Statement statement = con.createStatement();
 System.out.println("Database connectivity established
successfully!");

 String createTableQuery = "CREATE TABLE MOVIES (id
INT , title VARCHAR(255), genre VARCHAR(255),yearOfRelease
VARCHAR(255))";
 statement.executeUpdate(createTableQuery);
 System.out.println("Table MOVIES created
successfully!");

 String insertQuery = "INSERT INTO MOVIES
(id,title,genre,yearOfRelease) VALUES (1,'chandramukhi',
'horror','2003')";
 statement.executeUpdate(insertQuery);
 String insertq1 ="INSERT INTO MOVIES
(id,title,genre,yearOfRelease) VALUES (2,'arjunreddy',
'goddess','2019')";
 statement.executeUpdate(insertq1);
 System.out.println("Scenario 3: Data inserted
successfully!");

 String selectQuery = "SELECT * FROM MOVIES";
 ResultSet resultSet =
statement.executeQuery(selectQuery);
 System.out.println("Scenario 3: Data retrieved from
the table:");
 while (resultSet.next()) {
 System.out.println("ID: " +
resultSet.getInt("id") +
 ", Name: " + resultSet.getString("title")
+
 ", Age: " + resultSet.getString("genre")+
 ",year:"+resultSet.getString("yearOfRelea
se"));
 }

 String updateQuery = "UPDATE MOVIES SET genre =
'love'WHERE title = 'arjunreddy'";
 statement.executeUpdate(updateQuery);
 System.out.println("Scenario 3: Data updated
successfully!");

 System.out.println("Scenario 3: Data deleted
successfully!");
 String deleteQuery = "DELETE FROM MOVIES WHERE title
= 'chandramukhi'";
 statement.executeUpdate(deleteQuery);

 // Re-execute the select query
 ResultSet r = statement.executeQuery(selectQuery);
 while (r.next()) {
 System.out.println("ID: " + r.getInt("id") +
 ", Name: " + r.getString("title") +
 ", Age: " + r.getString("genre")+
 ",year:"+r.getString("yearOfRelease"));
 }

 String drop = "drop table MOVIES";
 statement.executeQuery(drop);
 String a = "commit";
 statement.executeQuery(a);

 resultSet.close();
 statement.close();
 con.close();

 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 }

Output:

You might also like