0% found this document useful (0 votes)
8 views7 pages

Java Graded Lab 1

Uploaded by

kirti21062021
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)
8 views7 pages

Java Graded Lab 1

Uploaded by

kirti21062021
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/ 7

Name -Aman kumar

Sap id -1000017717

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.

1 Enter up to 5 medicines details.

Medicine_name manufactured_date

Expiry_date Medicine_uses

Mysql command

CREATE DATABASE medicine_database;


USE medicine_database;

CREATE TABLE medicine (

Id INT(11) NOT NULL AUTO_INCREMENT,

Medicine_name VARCHAR(255) NOT NULL,

Manufactured_date DATE NOT NULL,

Expiry_date DATE NOT NULL,

Medicine_uses VARCHAR(255),

PRIMARY KEY (id)

);

INSERT INTO medicines (medicine_name,

Manufactured_date, expiry_date, medicine_uses)

VALUES

(‘Paracetamol’, ‘2022-01-01’, ‘2024-01-01’, ‘Pain

Relief’),

(‘Ibuprofen’, ‘2022-02-01’, ‘2024-02-01’, ‘Pain relief,


Fever reduction’),

(‘Amoxicillin’, ‘2022-03-01’, ‘2024-03-01’,

‘Antibiotic’),

(‘Loratadine’, ‘2022-04-01’, ‘2024-04-01’, ‘Allergy

Relief’),

(‘Omeprazole’, ‘2022-05-01’, ‘2024-05-01’, ‘Acid

Reflux treatment’);

Java source code

Import java.sql.*;

Public class MedicineDatabase {

Public static void main(String[] args) {

String url =

“jdbc:mysql://localhost:3306/medicine_db”;

String user = “root”;

String password = “password”;

Try {
// Establish database connection

Connection conn =

DriverManager.getConnection(url, user, password);

// Create a statement object to execute SQL

Queries

Statement stmt = conn.createStatement();

// Create the medicine table if it does not exist

String createTable = “CREATE TABLE IF NOT EXISTS

Medicine (medicine_name VARCHAR(50), “ +

“manufactured_date DATE,

Expiry_date DATE, medicine_uses VARCHAR(200))”;

Stmt.executeUpdate(createTable);

// Insert some medicine records into the table

String insertRecord1 = “INSERT INTO medicine

VALUES (‘Aspirin’, ‘2022-01-01’, ‘2023-01-01’, ‘Pain


Relief’)”;

String insertRecord2 = “INSERT INTO medicine

VALUES (‘Tylenol’, ‘2022-02-01’, ‘2023-02-01’, ‘Fever

Reducer’)”;

String insertRecord3 = “INSERT INTO medicine

VALUES (‘Benadryl’, ‘2022-03-01’, ‘2023-03-01’,

‘Antihistamine’)”;

Stmt.executeUpdate(insertRecord1);

Stmt.executeUpdate(insertRecord2);

Stmt.executeUpdate(insertRecord3);

// Retrieve all records from the medicine table

And display them

String query = “SELECT * FROM medicine”;

ResultSet rs = stmt.executeQuery(query);

While (rs.next()) {
String name = rs.getString(“medicine_name”);

String manufDate =

Rs.getString(“manufactured_date”);

String expDate = rs.getString(“expiry_date”);

String uses = rs.getString(“medicine_uses”);

System.out.println(name + “, “ + manufDate + “,

“ + expDate + “, “ + uses);

// Close the result set, statement, and connection

Objects

Rs.close();

Stmt.close();

Conn.close();

}
Catch (SQLException e) {

e.printStackTrace();

OUTPUT-

Aspirin, 2022-01-01, 2023-01-01, Pain relief

Tylenol, 2022-02-01, 2023-02-01, Fever reducer

Benadryl, 2022-03-01, 2023-03-01, Antihistamine

You might also like