// Java program to implement a simple JDBC application
import java.sql.*;
public class Geeks {
public static void main(String[] args)
{
// Database URL, username, and password
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";
String query = "INSERT INTO students (id, name) VALUES (109, 'bhatt')";
// Establish JDBC Connection
try {
// Load Type-4 Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
Connection c = DriverManager.getConnection(url, username, password);
// Create a statement
Statement st = c.createStatement();
// Execute the query
int count = st.executeUpdate(query);
System.out.println("Number of rows affected by this query: " + count);
// Close the connection
st.close();
c.close();
System.out.println("Connection closed.");
}
catch (ClassNotFoundException e) {
System.err.println("JDBC Driver not found: " + e.getMessage());
}
catch (SQLException e) {
System.err.println("SQL Error: " + e.getMessage());
}
}
}
A Statement object is used for general-purpose access to databases and is useful for executing static SQL statements at runtime.
Statement statement = connection.createStatement();
Once the Statement object is created, there are three ways to execute it:
A PreparedStatement represents a precompiled SQL statement that can be executed multiple times. It accepts parameterized SQL queries, with ? as placeholders for parameters, which can be set dynamically.
String query = "INSERT INTO people(name, age) VALUES(?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1,"Ayan");
ptstmt.setInt(2,25);
Once the PreparedStatement object is created, there are three ways to execute it:
// Java Program illustrating Prepared Statement in JDBC
import java.sql.*;
import java.util.Scanner;
class Geeks {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Scanner sc = new Scanner(System.in);
System.out.println("What age do you want to search?? ");
int age = sc.nextInt();
Connection con = DriverManager.getConnection(
"jdbc:mysql:///world", "root", "12345");
PreparedStatement ps = con.prepareStatement(
"select name from world.people where age = ?");
ps.setInt(1, age);
ResultSet res = ps.executeQuery();
while (res.next()) {
System.out.println("Name : " + res.getString(1));
}
}
catch (SQLException e) {
System.out.println(e);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
A CallableStatement is used to execute stored procedures in the database. Stored procedures are precompiled SQL statements that can be called with parameters. They are useful for executing complex operations that involve multiple SQL statements.
CallableStatement cstmt = con.prepareCall("{call ProcedureName(?, ?)}");
{call ProcedureName(?, ?)}: Calls a stored procedure named ProcedureName with
placeholders ? for input parameters.
// Java Program illustrating Callable Statement in JDBC
import java.sql.*;
public class Geeks {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager
.getConnection("jdbc:mysql:///world", "root", "12345");
CallableStatement cs = con.prepareCall("{call GetPeopleInfo()}");
ResultSet res = cs.executeQuery();
while (res.next()) {
System.out.println("Name : " + res.getString("name"));
System.out.println("Age : " + res.getInt("age"));
}
res.close();
cs.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
| Class/Interfaces | Description |
|---|---|
| DriverManager | Manages JDBC drivers and establishes database connections. |
| Connection | Represents a session with a specific database. |
| Statement | Used to execute static SQL queries. |
| PreparedStatement | Precompiled SQL statement, used for dynamic queries with parameters. |
| CallableStatement | Used to execute stored procedures in the database. |
| ResultSet | Represents the result set of a query, allowing navigation through the rows. |
| SQLException | Handles SQL-related exceptions during database operations. |
My simple library
..of useful code