0% found this document useful (0 votes)
72 views9 pages

Assignment No1-1

1. The document contains 5 programs demonstrating JDBC connectivity and operations using MySQL database. The programs cover connecting to a database, creating a table, executing select queries, filtering results, and inserting a record. 2. The first program establishes a connection and executes a simple select query to retrieve and display an employee name. 3. The second program creates an 'employee' table in the database with columns for id, name, department, and salary.

Uploaded by

Anurag Gupta
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)
72 views9 pages

Assignment No1-1

1. The document contains 5 programs demonstrating JDBC connectivity and operations using MySQL database. The programs cover connecting to a database, creating a table, executing select queries, filtering results, and inserting a record. 2. The first program establishes a connection and executes a simple select query to retrieve and display an employee name. 3. The second program creates an 'employee' table in the database with columns for id, name, department, and salary.

Uploaded by

Anurag Gupta
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

Assignment No : 1

Name : Abhishek kumar Verma


Roll No. : 2201330109004
Section : C

1. Install a database (MySQL or Oracle). Program to illustrate JDBC


connectivity. Program for maintaining database by sending queries.
Answer :
Installation Done

Program For Connectivity :


package db;

import java.sql.*; //step 1 IMPORT PACKAGE PHONE

public class Jdbc {


public static void main(String []args) throws Exception
{

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

String uname="root";

String pass="Root";

String query="SELECT emp_name FROM employee where emp_id=1";


Class.forName

("com.mysql.cj.jdbc.Driver");//step 2 class forname telusko LOAD (ADD LIBRARY MYSQL


CONNECTOR) N REGISTER DRIVER
Connection con=DriverManager.getConnection(url,uname,pass); //step3 CREATE
CONNECTION
Statement st=con.createStatement(); //step4 CREATE STATEMENT
ResultSet rs=st.executeQuery(query); //step5 EXECUTE QUERY

rs.next();
String name1=rs.getString("emp_name");
System.out.println(name1);//step6 PROCESS RESULT

st.close();
con.close();//step7 CLOSE CONNECTION

Output :

2.Program to create a demo table 'employee' using JDBC:


Java

Answer :
package db;

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

public class CreateEmployeeTable {


public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abhi";
String username = "root";
String password = "Root";
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sql = "CREATE TABLE employee (" +
"eno INT PRIMARY KEY AUTO_INCREMENT," +
"ename VARCHAR(50)," +
"department VARCHAR(50)," +
"sal DOUBLE)";
statement.executeUpdate(sql);
System.out.println("Table created successfully!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output :
3 .Program to execute and read select query using JDBC:

Answer :

package db;

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

public class ReadDataExample {


public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abhi";
String username = "root";
String password = "Root";
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sql = "SELECT * FROM employee";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int eno = resultSet.getInt("eno");
String ename = resultSet.getString("ename");
String department = resultSet.getString("department");
double sal = resultSet.getDouble("sal");
System.out.println("Employee ID: " + eno + ", Name: " + ename + ", Department: " + department + ",
Salary: " + sal);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output :

4. Program to display details of employees whose


department is IT:

Answer :
package db;

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

public class ITDepartmentEmployees {


public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abhi";
String username = "root";
String password = "Root";
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String sql = "SELECT eno, ename, sal FROM employee WHERE department='IT'";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int eno = resultSet.getInt("eno");
String ename = resultSet.getString("ename");
double sal = resultSet.getDouble("sal");
System.out.println("Employee ID: " + eno + ", Name: " + ename + ", Salary: " + sal);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output :
5. Program to insert a record in the database using
PreparedStatement interface:

Answer :
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertRecord {


public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abhi";
String username = "root";
String password = "Root";
connection = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO employee (ename, department, sal) VALUES (?, ?, ?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "John");
preparedStatement.setString(2, "IT");
preparedStatement.setDouble(3, 5000.0);
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new employee inserted successfully!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Output :

You might also like