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

Week 8 Assignment

The document provides code examples for connecting to a MySQL database using JDBC, executing queries, and performing CRUD operations (Create, Read, Update, Delete). It includes detailed Java code snippets for establishing a connection, executing a SELECT query, inserting, updating, and deleting records in a database. Each section emphasizes the importance of replacing placeholder values with actual database credentials and table/column names.

Uploaded by

gm2573
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Week 8 Assignment

The document provides code examples for connecting to a MySQL database using JDBC, executing queries, and performing CRUD operations (Create, Read, Update, Delete). It includes detailed Java code snippets for establishing a connection, executing a SELECT query, inserting, updating, and deleting records in a database. Each section emphasizes the importance of replacing placeholder values with actual database credentials and table/column names.

Uploaded by

gm2573
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

WEEK 8 WEEKLY TUTORIAL ASSIGNMENT

DECLARATIVE PROGRAMMING PARADIGM

JDBC CONNECTIVITY

RA2311026010866

Connecting to a Database import

java.sql.Connection; import

java.sql.DriverManager; import

java.sql.SQLException;

public class Main {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/yourDatabase"; // Replace 'yourDatabase'


with your database name

String user = "yourUsername"; // Replace with your database username

String password = "yourPassword"; // Replace with your database password

try {

Connection connection = DriverManager.getConnection(url, user, password);

if (connection != null) {

System.out.println("Connection established successfully!");

} catch (SQLException e)

{ System.out.println("Connection failed!");

e.printStackTrace();

}
}

Executing a Simple Query import

java.sql.Connection; import

java.sql.DriverManager; import

java.sql.ResultSet; import

java.sql.SQLException; import

java.sql.Statement;

public class Main {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/yourDatabase"; // Replace 'yourDatabase'


with your database name

String user = "yourUsername"; // Replace with your database username

String password = "yourPassword"; // Replace with your database password

try (Connection connection = DriverManager.getConnection(url, user, password);

Statement statement = connection.createStatement()) {

String query = "SELECT * FROM customers";

ResultSet resultSet = statement.executeQuery(query);

while (resultSet.next()) {

int id = resultSet.getInt("id"); // Replace "id" with your column name


String name = resultSet.getString("name"); // Replace "name" with your column
name

String email = resultSet.getString("email"); // Replace "email" with your column


name

// Display the retrieved data

System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);

} catch (SQLException e) {

e.printStackTrace();

Inserting Data

import java.sql.Connection; import

java.sql.DriverManager; import

java.sql.PreparedStatement; import

java.sql.SQLException;

public class Main {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/yourDatabase"; // Replace 'yourDatabase'


with your database name

String user = "yourUsername"; // Replace with your database username


String password = "yourPassword"; // Replace with your database password

String insertQuery = "INSERT INTO products (product_name, price, quantity) VALUES


(?, ?, ?)";

try (Connection connection = DriverManager.getConnection(url, user, password);

PreparedStatement preparedStatement =
connection.prepareStatement(insertQuery)) {

// Set values for the prepared statement

preparedStatement.setString(1, "New Product"); // Replace with the product name

preparedStatement.setDouble(2, 19.99); // Replace with the product price

preparedStatement.setInt(3, 10); // Replace with the product quantity

// Execute the insert statement

int rowsAffected = preparedStatement.executeUpdate();

System.out.println(rowsAffected + " record(s) inserted successfully!");

} catch (SQLException e) {

e.printStackTrace();

Updating Data

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class Main {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/yourDatabase"; // Replace 'yourDatabase'


with your database name

String user = "yourUsername"; // Replace with your database username

String password = "yourPassword"; // Replace with your database password

String updateQuery = "UPDATE orders SET quantity = ?, status = ? WHERE order_id = ?";

try (Connection connection = DriverManager.getConnection(url, user, password);

PreparedStatement preparedStatement =
connection.prepareStatement(updateQuery)) {

preparedStatement.setInt(1, 5); // New quantity

preparedStatement.setString(2, "Shipped"); // New status

preparedStatement.setInt(3, 1); // Order ID to update

int rowsAffected = preparedStatement.executeUpdate();

System.out.println(rowsAffected + " record(s) updated successfully!");

} catch (SQLException e) {

e.printStackTrace();
}

Deleting Data

import java.sql.Connection; import

java.sql.DriverManager; import

java.sql.PreparedStatement; import

java.sql.SQLException;

public class Main {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/yourDatabase"; // Replace 'yourDatabase'


with your database name

String user = "yourUsername"; // Replace with your database username

String password = "yourPassword"; // Replace with your database password

String deleteQuery = "DELETE FROM employees WHERE employee_id = ?";

try (Connection connection = DriverManager.getConnection(url, user, password);

PreparedStatement preparedStatement =
connection.prepareStatement(deleteQuery))

{ preparedStatement.setInt(1, 1); // Employee ID to delete

int rowsAffected = preparedStatement.executeUpdate();


System.out.println(rowsAffected + " record(s) deleted successfully!");

} catch (SQLException e) {

e.printStackTrace();

You might also like