Show List
JDBC
JDBC (Java Database Connectivity) is a Java API used to connect and execute SQL statements on a database. It provides a standard API for accessing relational databases and is used to perform common database operations such as creating tables, inserting data, retrieving data, updating data, and deleting data.
Here are some examples of how JDBC can be used in Java:
- Establishing a Connection: To start using JDBC, you need to establish a connection to a database. This can be done using the DriverManager class and the getConnection() method.
import java.sql.*;
public class JDBCExample {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/testDB";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected to database successfully...");
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
- Executing a SELECT Statement: To retrieve data from a database, you can execute a SELECT statement using the Statement or PreparedStatement interface. Here's an example of how to retrieve all rows from a table named "employees":
import java.sql.*;
public class JDBCExample {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/testDB";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT id, first, last, age FROM employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
System.out.println("ID: " + id);
System.out.println(", Age: " + age);
System.out.println(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
- Executing an INSERT Statement: To insert data into a database, you can execute an INSERT statement using the Statement or PreparedStatement interface. Here's an example of how to insert a row into a table named "employees":
import java.sql.*;
public class JDBCExample {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/testDB";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO employees " +
"VALUES (100, 'John', 'Doe', 25)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
}catch(SQLException se){
se.printStackTrace();
}
Leave a Comment