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

Dbms 8

The document provides a Java example of connecting to a MySQL database using JDBC. It includes code for establishing a connection, executing a SQL query to retrieve employee data, and handling exceptions. The code also demonstrates proper resource management by closing the connection and statement at the end.

Uploaded by

Ajit Nagargoje
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)
13 views4 pages

Dbms 8

The document provides a Java example of connecting to a MySQL database using JDBC. It includes code for establishing a connection, executing a SQL query to retrieve employee data, and handling exceptions. The code also demonstrates proper resource management by closing the connection and statement at the end.

Uploaded by

Ajit Nagargoje
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/ 4

Assignment 8

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.SQLException;

public class MySQLJDBCExample {

// JDBC driver name and database URL

static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost:3306/testdb"; // Replace 'testdb' with your


database name

// Database credentials

static final String USER = "root"; // Replace with your MySQL username

static final String PASS = “ajit@mysql"; // Replace with your MySQL password public

static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

try {

// Register JDBC driver

Class.forName(JDBC_DRIVER);

// Open a connection

System.out.println("Connecting to database...");

conn = DriverManager.getConnection(DB_URL, USER, PASS);

if (conn != null) {

System.out.println("Connected to the database successfully!");

} else {

System.out.println("Failed to connect to the database.");

// Execute a query
System.out.println("Creating statement...");

stmt = conn.createStatement();

String sql = "SELECT id, name, salary FROM employees"; // Change according to your table

ResultSet rs = stmt.executeQuery(sql);

// Extract data from result

set while (rs.next()) {

// Retrieve by column

name int id =

rs.getInt("id");

String name = rs.getString("name");

double salary =

rs.getDouble("salary");

// Display values

System.out.print("ID: " + id);

System.out.print(", Name: " + name);

System.out.println(", Salary: " + salary);

// Clean-up

environment rs.close();

stmt.close();

conn.close();

} catch (SQLException se) {

// Handle errors for

JDBC

se.printStackTrace();

} catch (Exception e) {

// Handle errors for

Class.forName

e.printStackTrace();

} finally {

// Finally block to close

resources try {

if (stmt != null) stmt.close();

} catch (SQLException se2) {


}
try {

if (conn != null) conn.close();

} catch (SQLException se)

{ se.printStackTrace();

System.out.println("Goodbye!");

You might also like