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();