DBMS LAB PROGRAMS (1)
DBMS LAB PROGRAMS (1)
Create a table and perform the search operation on table using indexing and
nonindexing techniques.
-- Create Table
employee_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
VALUES
-- Indexed Search
______________________________________________________________________________
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("com.mysql.cj.jdbc.Driver");
statement.close();
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
System.out.println("Connection closed.");
se.printStackTrace();
}
}
Output:
14. Write a Java program to connect to a database using JDBC and insert values
into it.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Class.forName("com.mysql.cj.jdbc.Driver");
preparedStatement.setInt(1, 6);
preparedStatement.setString(3, "HR");
preparedStatement.setDouble(4, 6200.00);
if (rowsAffected > 0) {
} else {
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
System.out.println("Connection closed.");
se.printStackTrace();
Output:
15. Write a Java program to connect to a database using JDBC and delete values
from it.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
preparedStatement.setInt(1, 6);
if (rowsAffected > 0) {
} else {
System.out.println("No data found to delete.");
preparedStatement.close();
se.printStackTrace();
} catch (Exception e) {
for Class.forName
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
System.out.println("Connection closed.");
se.printStackTrace();
Output: