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

DBMS Exp-14

This document outlines a Java program that connects to a database using JDBC to insert and retrieve student records. It specifies the required software, including JDK and JDBC driver, and provides source code for establishing a connection, inserting data, and displaying results. The program includes exception handling and resource management practices.

Uploaded by

nagasair.12345
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)
16 views3 pages

DBMS Exp-14

This document outlines a Java program that connects to a database using JDBC to insert and retrieve student records. It specifies the required software, including JDK and JDBC driver, and provides source code for establishing a connection, inserting data, and displaying results. The program includes exception handling and resource management practices.

Uploaded by

nagasair.12345
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
You are on page 1/ 3

DATABASE MANAGEMENT SYSTEMS LAB

EXP: 14 Write a Java program to connect to a database using


Date: JDBC and insert values into it

Aim: Write a Java program to connect to a database using JDBC and


insert values into it

SOLUTION:

1. Software Requirements:
 Java Development Kit (JDK): Version 8 or later
 Database: XAMPP Server
 JDBC Driver: Corresponding JDBC driver for the selected database (e.g.,
MySQL Connector/J for MySQL) //mysql-connector-j-9.1.0.jar
 Database Credentials: Database URL, username, and password
2. Functional Requirements:
 Establish a connection to the database using JDBC
 Execute a simple SQL query (e.g., retrieving data from a table)
 Display query results in the console
 Handle exceptions properly (e.g., SQLExceptions)
 Ensure proper resource management (closing connections)

Source code

import java.sql.*; // Import SQL package for database operations


import java.util.Scanner; // Import Scanner for user input

public class InsertAndPrintAll {


public static void main(String[] args) {

// Database connection details


String url = "jdbc:mysql://localhost:3306/college"; // Database URL

Page 1
String username = "root"; // Database username
String password = ""; // Database password (leave empty if none)

try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");

// Establish the database connection


Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("Connected to the database successfully!");

// Create Scanner object to take user input


Scanner scanner = new Scanner(System.in);

// Get Student ID from the user


System.out.print("Enter Student ID: ");
int stuid = scanner.nextInt();
scanner.nextLine(); // Consume the leftover newline character

// Get Student Name from the user


System.out.print("Enter Student Name: ");
String stuname = scanner.nextLine();

// SQL query to insert data into the student table


String insertQuery = "INSERT INTO student (Stuid, Stuname) VALUES
(?, ?)";
PreparedStatement insertStatement =
connection.prepareStatement(insertQuery);
insertStatement.setInt(1, stuid); // Set Student ID
insertStatement.setString(2, stuname); // Set Student Name

// Execute the insert query


insertStatement.executeUpdate();
System.out.println("Student inserted successfully!");

// SQL query to retrieve all student records


String selectQuery = "SELECT * FROM student";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectQuery);

// Display all student records


System.out.println("\n Records in Student table are:");
while (resultSet.next()) {
System.out.println("Stuid: " + resultSet.getInt("Stuid") + ", Stuname:
" + resultSet.getString("Stuname"));
Page 2
}
// Close all resources
scanner.close();
connection.close();
} catch (Exception e) {
// Handle any errors
e.printStackTrace();
}
}
}

Complile and Execution

C:\Users\SRIKANTH\Desktop\jdbc> javac -cp .;mysql-connector-java-


9.1.0.jar InsertAndPrintAll.java

C:\Users\SRIKANTH\Desktop\jdbc> java -cp .;mysql-connector-j-9.1.0.jar


InsertAndPrintAll

Page 3

You might also like