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

Ass7 Solution FYMCA Sem-2 Java Lab

This document outlines a Java programming laboratory assignment focused on database connectivity and SQL query execution using JDBC. It details the objectives, software requirements, and steps for establishing a connection to a MySQL database, executing SQL queries, and retrieving data. The provided Java program demonstrates these concepts by connecting to a 'college' database and displaying student records.

Uploaded by

pratikshabhad37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Ass7 Solution FYMCA Sem-2 Java Lab

This document outlines a Java programming laboratory assignment focused on database connectivity and SQL query execution using JDBC. It details the objectives, software requirements, and steps for establishing a connection to a MySQL database, executing SQL queries, and retrieving data. The provided Java program demonstrates these concepts by connecting to a 'college' database and displaying student records.

Uploaded by

pratikshabhad37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Laboratory Assignment-7

Title: Database Connectivity and SQL Query Execution in Java


Problem Statement - Write a program to connect to any database and to execute the
SQL query operation.

Objective:

 To establish a connection between a Java application and a database.


 To execute SQL queries using Java.
 To retrieve and display data from the database table using JDBC.

Software and Hardware Requirements

 Java Development Kit (JDK)


 MySQL Database
 MySQL Connector/J (JDBC Driver)
 IDE (Eclipse / NetBeans / IntelliJ) or Command Prompt
 Windows/Linux/Mac

Theory

 JDBC (Java Database Connectivity) is used to connect Java with databases.


 DriverManager is used to get database connection.
 Connection, Statement, ResultSet are used for SQL execution.

Steps of JDBC Connectivity

1. Import JDBC packages


2. Load and register the driver
3. Establish connection with database
4. Create statement object
5. Execute SQL query
6. Process result
7. Close connection

Program:

Database and Table creation in MySql =>

CREATE DATABASE college;

USE college;

CREATE TABLE student(

id INT PRIMARY KEY,


name VARCHAR(50),

marks INT

);

INSERT INTO student VALUES(1,'Amit',85);

INSERT INTO student VALUES(2,'Neha',90);

INSERT INTO student VALUES(3,'Rahul',78);

// Java Program: Database Connectivity and SQL Query Execution

import [Link].*;

public class DBConnectionExample

public static void main(String args[])

try

// Load JDBC Driver

[Link]("[Link]");

// Establish Connection

Connection con = [Link](

"jdbc:mysql://localhost:3306/college","root","password");

// Create Statement

Statement stmt = [Link]();

// Execute SQL Query


ResultSet rs = [Link]("SELECT * FROM student");

// Display Data

[Link]("ID Name Marks");

while([Link]())

[Link](

[Link](1) + " " +

[Link](2) + " " +

[Link](3));

// Close Connection

[Link]();

catch(Exception e)

[Link](e);

}
Output:
ID Name Marks
1 Amit 85
2 Neha 90
3 Rahul 78
Conclusion
The program successfully demonstrates how a Java application connects to a database using
JDBC and executes an SQL query to retrieve records. This method allows Java programs to
interact efficiently with relational databases.

You might also like