0% found this document useful (0 votes)
4 views3 pages

Graded Lab 3

Java program

Uploaded by

divyanshu18603
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)
4 views3 pages

Graded Lab 3

Java program

Uploaded by

divyanshu18603
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/ 3

Laboratory Assignment Sheet

Assignment ID Date of Submission


(as per the Assignment Assessment Release – Deadline – 21-
policy Title- Graded Submission Method- Group/ 21-March March 2023,
guidelines) lab 3 Mode- Online Internal Individual Weightage 2023 1 PM

Instructions:

 Download this document and solve them during laboratory time.


 Do not copy the code of other students.

Due Date: 21-March 2023

Problems:

1. Write a program in Java to create database connectivity with a database and


display the records of the table.
a. create a table in MySQL
b. retrieve the all the content of the table and display it
c. connect the database to your java program and retrieve all the table
contents.

Note:
1. Paste the outputs of each step you perform.
2. Below given are the questions.
3. The LAST DIGIT of Student Sap Id is given with corresponding database
table details.

1. Sap id ending with digit 0,5 will create the table of employee with columns.
Enter up to 5 employee details only.

employee_id employee_name, DOB joining_date

2. Sap id ending with digit 1,2,8,9 will create the table of medicines with
columns. Enter up to 5 medicines details.

medicine_name manufactured_date expiry_date Medicine_uses


Laboratory Assignment Sheet

3. Sap id ending with digit 3,4,6,7 will create the table of students with
columns. Enter up to 5 students details.

student_name roll_number Sap_Id favourite_subject

Name: Divyanshu Poonia


Sap:1000016147

Source Code:
import java.sql.*;

public class StudentDatabaseExample {


public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {

conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase",
"username", "password");

stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE students (student_name VARCHAR(50),
roll_number INT, Sap_Id INT, favourite_subject VARCHAR(50))");

stmt.executeUpdate("INSERT INTO students VALUES ('John Doe', 101,


1234567, 'Mathematics')");
stmt.executeUpdate("INSERT INTO students VALUES ('Jane Smith', 102,
1234568, 'English')");
stmt.executeUpdate("INSERT INTO students VALUES ('Bob Johnson', 103,
1234569, 'Science')");
stmt.executeUpdate("INSERT INTO students VALUES ('Sara Lee', 104,
1234570, 'History')");
stmt.executeUpdate("INSERT INTO students VALUES ('Tom Adams', 105,
1234571, 'Geography')");

rs = stmt.executeQuery("SELECT * FROM students");


Laboratory Assignment Sheet

while (rs.next()) {
System.out.println(rs.getString("student_name") + "\t" +
rs.getInt("roll_number") + "\t" + rs.getInt("Sap_Id") + "\t" +
rs.getString("favourite_subject"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
if (rs != null)
{
rs.close();
}
if (stmt != null)
{
stmt.close();
}
if (conn != null)
{
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output :

You might also like