DEPARTMENT OF COMPUTER ENGINEERING
Subject: AJP Subject Code: 22517
th
Semester: 5 Semester Course: Computer Engineering
Laboratory No: V119 Name of Subject Teacher: Prasad Koyande
Name of Student: Ekrama Ansari Roll Id: 22203C0010
Experiment No: 18
Title of Experiment Write a program to insert and retrieve the data from database using JDBC
XII. EXCERSISE:
1. Develop a program to create employee table in database having two columns “emp_id”
and “emp_name”.
CODE: /*
* To change this license header, choose License Headers in Project Properties. * To
change this template file, choose Tools | Templates and open the template in the
editor.
*/
package javaapplication6;
import java.sql.*;
/**
* @author student3
*/
public class JavaApplication6 { static final String JDBC_DRIVER =
"com.mysql.jdbc.Driver"; static final String DB_URL =
"jdbc:mysql://localhost/employee"; static final String USER =
"root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Page | 1
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected to database successfully...");
// STEP 4: Execute an insert query
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO employee (emp_name) VALUES ('ADITYA')";
stmt.executeUpdate(sql);
sql = "INSERT INTO employee (emp_name) VALUES ('SIDDHARTH')";
stmt.executeUpdate(sql);
// STEP 5: Execute a select query
String query = "SELECT * FROM employee";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
// Iterate through the result set
while (rs.next()) {
int empid = rs.getInt("empid");
System.out.format("Employee ID: %s\n", empid);
}
st.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// Close resources
try { if
(stmt != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
try {
if (conn != null)
conn.close();
Page | 2
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
OUTPUT:
2. Develop a program to display the name and roll_no of students from “student table”
having percentage > 70.
CODE:
/*
* To change this license header, choose License Headers in Project Properties. * To
change this template file, choose Tools | Templates and open the template in the
editor.
*/
Page | 3
package javaapplication7;
import java.sql.*;
public class JavaApplication7 { static final String JDBC_DRIVER
= "com.mysql.jdbc.Driver"; static final String DB_URL =
"jdbc:mysql://localhost/student"; static final String USER =
"root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected to database successfully...");
// STEP 4: Execute a select query
System.out.println("Getting records from the table...");
stmt = conn.createStatement();
String query = "SELECT * FROM stud WHERE percentage > 70";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
// Iterate through the result set
while (rs.next()) {
int rollNo = rs.getInt("roll_no");
String name = rs.getString("stud_name");
float percentage = rs.getFloat("percentage");
System.out.format("Roll No: %s, Name: %s, Percentage: %.2f\n", rollNo, name,
percentage);
}
st.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
Page | 4
} finally {
// Close resources
try { if
(stmt != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
OUTPUT:
Grade and Process Related Product Related Dated Sign
Dated (35) (15)
Signature of
Teacher
Page | 5