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

Untitled Document

Uploaded by

dekud9556
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)
10 views3 pages

Untitled Document

Uploaded by

dekud9556
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

Casestudy: JDBC

Aim;
To implement JDBC concepts on a case study.
Program:

import java.sql.*;

public class CustomerDatabase {


private static final String URL = "jdbc:mysql://localhost:3306/your_database_name";
private static final String USER = "your_username";
private static final String PASSWORD = "your_password";

public static void main(String[] args) {


try (Connection con = DriverManager.getConnection(URL, USER, PASSWORD)) {
createTable(con);
insertRecord(con, 1, "Company A", 101, 5000);
insertRecord(con, 2, "Company B", 102, 6000);
showRecords(con);
insertRecord(con, 3, "Company C", 103, 8000);
showRecords(con);
updateRecord(con, 3, "Updated Company C", 104, 9000);
showRecords(con);
deleteRecord(con, 3);
showRecords(con);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void createTable(Connection con) throws SQLException {


String createTableSQL = "CREATE TABLE IF NOT EXISTS CUSTOMERS (" +
"CUST_NUM INT PRIMARY KEY," +
"COMPANY VARCHAR(100)," +
"CUST_REP INT," +
"CREDIT_LIMIT INT" +
")";
try (Statement stmt = con.createStatement()) {
stmt.execute(createTableSQL);
System.out.println("Table CUSTOMERS created successfully.");
}
}

private static void showRecords(Connection con) throws SQLException {


String query = "SELECT * FROM CUSTOMERS";
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query)) {
System.out.println("CUST_NUM" + "\tCOMPANY" + "\t\tCUST_REP" +
"\tCREDIT_LIMIT");
while (rs.next()) {
System.out.println(rs.getInt("CUST_NUM") + "\t\t" +
rs.getString("COMPANY") + "\t" +
rs.getInt("CUST_REP") + "\t" +
rs.getInt("CREDIT_LIMIT"));
}
}
}

private static void insertRecord(Connection con, int custNum, String company, int custRep, int
creditLimit) throws SQLException {
String insertSQL = "INSERT INTO CUSTOMERS (CUST_NUM, COMPANY, CUST_REP,
CREDIT_LIMIT) VALUES (?, ?, ?, ?)";
try (PreparedStatement pstmt = con.prepareStatement(insertSQL)) {
pstmt.setInt(1, custNum);
pstmt.setString(2, company);
pstmt.setInt(3, custRep);
pstmt.setInt(4, creditLimit);
pstmt.executeUpdate();
System.out.println("Record inserted successfully.");
}
}

private static void updateRecord(Connection con, int custNum, String newCompany, int
newCustRep, int newCreditLimit) throws SQLException {
String updateSQL = "UPDATE CUSTOMERS SET COMPANY = ?, CUST_REP = ?,
CREDIT_LIMIT = ? WHERE CUST_NUM = ?";
try (PreparedStatement pstmt = con.prepareStatement(updateSQL)) {
pstmt.setString(1, newCompany);
pstmt.setInt(2, newCustRep);
pstmt.setInt(3, newCreditLimit);
pstmt.setInt(4, custNum);
pstmt.executeUpdate();
System.out.println("Record updated successfully.");
}
}
private static void deleteRecord(Connection con, int custNum) throws SQLException {
String deleteSQL = "DELETE FROM CUSTOMERS WHERE CUST_NUM = ?";
try (PreparedStatement pstmt = con.prepareStatement(deleteSQL)) {
pstmt.setInt(1, custNum);
pstmt.executeUpdate();
System.out.println("Record deleted successfully.");
}
}
}

Output:
Table CUSTOMERS created successfully.
Record inserted successfully.
Record inserted successfully.
CUST_NUM COMPANY CUST_REP CREDIT_LIMIT
1 Company A 101 5000
2 Company B 102 6000
Record inserted successfully.
CUST_NUM COMPANY CUST_REP CREDIT_LIMIT
1 Company A 101 5000
2 Company B 102 6000
3 Company C 103 8000
Record updated successfully.
CUST_NUM COMPANY CUST_REP CREDIT_LIMIT
1 Company A 101 5000
2 Company B 102 6000
3 Updated Company C 104 9000
Record deleted successfully.
CUST_NUM COMPANY CUST_REP CREDIT_LIMIT
1 Company A 101 5000
2 Company B 102 6000

You might also like