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

DBMS LAB PROGRAMS (1)

The document provides SQL commands to create a table named 'employees', insert sample data, and perform search operations using both indexed and non-indexed techniques. It also includes Java programs demonstrating how to connect to a database using JDBC, insert data into the table, and delete data from it. Each Java program handles database connections and operations while managing exceptions and closing connections properly.
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)
30 views

DBMS LAB PROGRAMS (1)

The document provides SQL commands to create a table named 'employees', insert sample data, and perform search operations using both indexed and non-indexed techniques. It also includes Java programs demonstrating how to connect to a database using JDBC, insert data into the table, and delete data from it. Each Java program handles database connections and operations while managing exceptions and closing connections properly.
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/ 9

12.

Create a table and perform the search operation on table using indexing and
nonindexing techniques.

-- Create Table

CREATE TABLE employees (

employee_id INT PRIMARY KEY,

employee_name VARCHAR(50),

department VARCHAR(50),

salary DECIMAL(10, 2)

);

-- Insert Sample Data

INSERT INTO employees (employee_id, employee_name, department, salary)

VALUES

(1, 'John Doe', 'HR', 5000.00),

(2, 'Jane Smith', 'Finance', 6000.00),

(3, 'Sam Brown', 'IT', 7000.00),

(4, 'Chris Johnson', 'Marketing', 5500.00),

(5, 'Anna Williams', 'Finance', 7500.00);

-- Non-indexed Search (Full Table Scan)

SELECT * FROM employees

WHERE department = 'Finance';


-- Create Index on 'department' column

CREATE INDEX idx_department ON employees(department);

-- Indexed Search

SELECT * FROM employees

WHERE department = 'Finance';

______________________________________________________________________________

13. Write a Java program that connects to a database using JDBC.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCExample {

public static void main(String[] args) {

String url = "jdbc:mysql ;

String username = "your_username";

String password = "your_password”;

String query = "SELECT * FROM employees";

Connection connection = null;


try {

Class.forName("com.mysql.cj.jdbc.Driver");

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

System.out.println("Connected to the database successfully!");

Statement statement = connection.createStatement();

statement.close();

} catch (SQLException se) {

se.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (connection != null) {

connection.close();

System.out.println("Connection closed.");

} catch (SQLException se) {

se.printStackTrace();

}
}

Output:

Connected to the database successfully

Executing query: SELECT * FROM employees

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;

public class JDBCInsertExample {

public static void main(String[] args) {

String url = "jdbc:mysql

String username = "your_username";

String password = "your_password";

String insertQuery = "INSERT INTO employees (employee_id, employee_name,


department, salary) VALUES (?, ?, ?, ?)";

Connection connection = null;


try {

Class.forName("com.mysql.cj.jdbc.Driver");

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

System.out.println("Connected to the database successfully!");

PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);

preparedStatement.setInt(1, 6);

preparedStatement.setString(2, "Alice Green");

preparedStatement.setString(3, "HR");

preparedStatement.setDouble(4, 6200.00);

int rowsAffected = preparedStatement.executeUpdate();

if (rowsAffected > 0) {

System.out.println("Data inserted successfully!");

} else {

System.out.println("Failed to insert data.");

preparedStatement.close();

} catch (SQLException se) {


se.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (connection != null) {

connection.close();

System.out.println("Connection closed.");

} catch (SQLException se) {

se.printStackTrace();

Output:

Connected to the database successfully

Data inserted successfully

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;

public class JDBCDeleteExample {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/your_database_name";

String username = "your_username";

String password = "your_password";

String deleteQuery = "DELETE FROM employees WHERE employee_id = ?";

Connection connection = null;

try {

Class.forName("com.mysql.cj.jdbc.Driver");

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

System.out.println("Connected to the database successfully!");

PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery);

preparedStatement.setInt(1, 6);

int rowsAffected = preparedStatement.executeUpdate();

if (rowsAffected > 0) {

System.out.println("Data deleted successfully!");

} else {
System.out.println("No data found to delete.");

preparedStatement.close();

} catch (SQLException se) {

se.printStackTrace();

} catch (Exception e) {

for Class.forName

e.printStackTrace();

} finally {

try {

if (connection != null) {

connection.close();

System.out.println("Connection closed.");

} catch (SQLException se) {

se.printStackTrace();

Output:

Connected to the database successfully

Data deleted successfully

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy