0% found this document useful (0 votes)
1 views12 pages

Database Connection Hand Out

The document provides a step-by-step guide for creating a CRUD application using Java and MySQL. It includes instructions for setting up a database named 'crud', importing necessary SQL classes, and executing an SQL insert statement to add user data. The code demonstrates how to establish a database connection, handle user input, and manage exceptions during database operations.

Uploaded by

Gray Smith
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)
1 views12 pages

Database Connection Hand Out

The document provides a step-by-step guide for creating a CRUD application using Java and MySQL. It includes instructions for setting up a database named 'crud', importing necessary SQL classes, and executing an SQL insert statement to add user data. The code demonstrates how to establish a database connection, handle user input, and manage exceptions during database operations.

Uploaded by

Gray Smith
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/ 12

Go to chrome or simply click the Admin button at the XAMPP Control Panel

Create Database

Name the Database >> crud


Make sure that MySQL and JDK are already added in the Libraries
package jdbccrud;
import java.util.Scanner;
import java.sql.Connection; //This imports the connection class from the java.sql
package which represents a connection to a database
import java.sql.DriverManager; //This imports the DriverManager class, which is
used to manage a list of database drivers and establish a connection to a database
import java.sql.PreparedStatement; //This imports the PreparedStatement class,
which represents a precompiled SQL statement that can be executed multiple times
with different parameters
import java.sql.SQLException; //This imports the SQLExemption class, which handles
SQL exceptions and provides information about database-related errors.
public class JdbcCrud { //This line declares the JdbcCrud class, which contains the
code for performing a simple database operation (CRUD). public means it can be
accessed from outside the class.

public static void main(String[] args) { //The main method is the entry point of
any Java application. The program will start executing from here
Scanner sc = new Scanner(System.in);

System.out.println("Enter name: ");


String name = sc.nextLine();

System.out.println("Enter city: ");


String city = sc.nextLine();

System.out.println("Enter age: ");


int age = sc.nextInt();

String url = "jdbc:mysql://localhost:3306/crud"; //gawa muna ng database sa


mysql then palitan ung "your_database"
//This is a variable holding the URL of the database to which the application will
connect. In this case, it’s a MySQL database hosted locally (localhost) on port 3306,
and it’s trying to connect to a database named crud.
String username = "root"; //This holds the database username used to
authenticate the connection. The default username for MySQL is often root.
String password = ""; //This holds the password used to authenticate the
connection. Here, it’s empty (""), which assumes no password is set for the root
user.
// Sample data

// SQL insert statement


String sql = "INSERT INTO user (name, age, city) VALUES (?, ?, ?)";
//SQL query used to insert a new record into the user table ex. namae and age
// Try-with-resources to manage connection
try (Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// This lines uses try-with-resources, which automatically closes resources like
connection and PreparedStatement after the block of code executes, even if an
exception occurs.
// Connection conn = DriverManager.getConnection(url, username, password): This
establishes a connection to the MySQL database using the provided url, username,
and password.
//PreparedStatement pstmt = conn.prepareStatement(sql): This creates a
PreparedStatement object to execute the SQL query on the connected database
// Set parameters
pstmt.setString(1, name); // sets place holder name variable ("John Doe")
pstmt.setInt(2, age);//sets the second placeholder in SQL query to the value
of the age varaiable.
pstmt.setString(3, city);

// Execute insert
int rowsInserted = pstmt.executeUpdate();// executes the SQL statement
which is INSERT in this case. It will return 1 if the statement is successful
if (rowsInserted > 0) {
System.out.println("Data inserted successfully!");
//This checks if the number of rows affected by the INSERT operation is
greater than 0, indicating that the insert was successful. If successful, it prints "Data
inserted successfully!" to the console.
}

} catch (SQLException e) { //This block catches any SQL exceptions that occur
during the execution of the try block. If any error occurs (like a problem connecting
to the database or an invalid query), the exception is caught here.
e.printStackTrace();
// this prints the details of the exception such as error messages to the
console, helping with debugging
}
}
}

You might also like