0% found this document useful (0 votes)
12 views

Expt 4 &9

The document shows code for a Java program that connects to a MySQL database, creates a table, inserts data, displays data, deletes data, updates data, and closes connections.

Uploaded by

hrtec
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)
12 views

Expt 4 &9

The document shows code for a Java program that connects to a MySQL database, creates a table, inserts data, displays data, deletes data, updates data, and closes connections.

Uploaded by

hrtec
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/ 4

Code :

package Gui;

import java.sql.*;

public class DatabaseApp {


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

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

// variables
final String url = "jdbc:mysql://localhost:3306/pvg";
final String user = "root";
final String password = "root";

// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(url, user, password);
System.out.println("\nConnected to Database ");

// Create a table
System.out.println("\nCreating table...");
stmt = conn.createStatement();
String sql = "CREATE TABLE EMPLOYEES " +
"(ID INT PRIMARY KEY NOT NULL, " +
" NAME VARCHAR(255), " +
" AGE INT, " +
" ADDRESS VARCHAR(255))";
stmt.executeUpdate(sql);
System.out.println("\nTable created successfully.");

// Insert data
System.out.println("\nInserting data...");
sql = "INSERT INTO EMPLOYEES (ID, NAME, AGE, ADDRESS) " +
"VALUES (1, 'Kishor', 20, 'Pune')";
stmt.executeUpdate(sql);
System.out.println("Data inserted successfully.");

sql = "INSERT INTO EMPLOYEES (ID, NAME, AGE, ADDRESS) " +


"VALUES (2, 'John', 21, 'Pune')";
stmt.executeUpdate(sql);
System.out.println("Data inserted successfully.");
sql = "INSERT INTO EMPLOYEES (ID, NAME, AGE, ADDRESS) " +
"VALUES (3, 'Jack', 22, 'Nagpur')";
stmt.executeUpdate(sql);
System.out.println("\nData inserted successfully.");
sql = "INSERT INTO EMPLOYEES (ID, NAME, AGE, ADDRESS) " +
"VALUES (4, 'Ram', 20, 'Nashik')";
stmt.executeUpdate(sql);
System.out.println("\nData inserted successfully.");
// Display data
System.out.println("\nDisplaying data...");
sql = "SELECT * FROM EMPLOYEES";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
int age = rs.getInt("AGE");
String address = rs.getString("ADDRESS");
System.out.println("ID: " + id + " Name: " + name + ",
Age: " + age +
", Address: " + address);
}

// Delete data
System.out.println("\nDeleting data...");
sql = "DELETE FROM EMPLOYEES WHERE ID=3";
stmt.executeUpdate(sql);
System.out.println("\nData deleted successfully.");

//sowing Deleted Data


sql = "SELECT * FROM EMPLOYEES";
ResultSet rs1 = stmt.executeQuery(sql);
while (rs1.next()) {
int id = rs1.getInt("ID");
String name = rs1.getString("NAME");
int age = rs1.getInt("AGE");
String address = rs1.getString("ADDRESS");
System.out.println("ID: " + id + " Name: " + name + ",
Age: " + age +
", Address: " + address);
}
// Update data
System.out.println("\nUpdating data...");
sql = "UPDATE EMPLOYEES SET ADDRESS='BEED' WHERE ID=1";
stmt.executeUpdate(sql);
System.out.println("\nData updated successfully.");

// display updated data


sql = "SELECT * FROM EMPLOYEES";
ResultSet rs2 = stmt.executeQuery(sql);
while (rs2.next()) {
int id = rs2.getInt("ID");
String name = rs2.getString("NAME");
int age = rs2.getInt("AGE");
String address = rs2.getString("ADDRESS");
System.out.println("ID: " + id + " Name: " + name +
", Age: " + age + ", Address: " + address);
}
// 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 used to close resources
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
} // nothing we can do
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
}
}}
Output :

Connecting to database...

Connected to Database

Creating table...

Table created successfully.

Inserting data...
Data inserted successfully.
Data inserted successfully.

Data inserted successfully.

Data inserted successfully.

Displaying data...
ID: 1 Name: Kishor, Age: 20, Address: Pune
ID: 2 Name: John, Age: 21, Address: Pune
ID: 3 Name: Jack, Age: 22, Address: NagpurID: 4
Name: Ram, Age: 20, Address: Nashik

Deleting data...

Data deleted successfully.


ID: 1 Name: Kishor, Age: 20, Address: Pune
ID: 2 Name: John, Age: 21, Address: Pune
ID: 4 Name: Ram, Age: 20, Address: Nashik

Updating data...

Data updated successfully.


ID: 1 Name: Kishor, Age: 19, Address: BEED
ID: 2 Name: John, Age: 21, Address: Pune
ID: 4 Name: Ram, Age: 20, Address: Nashik

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy