JDBC Driver
Rajesh Kumar
SQL Developer at Aven Data || Ex BNY Mellon || Full stack Developer || Java + spring boot || React js
1. Load the Driver :
Class.forName("oracle.jdbc.driver.OracleDriver");
2. Connection :
String url = "jdbc:mysql://localhost:3306/yourDatabase";
Connection conn = DriverManager.getConnection(url, "yourUsername", "yourPassword");
3.Create Statement & Perform CRUD:
Statement stmt = conn.createStatement();
4.Create (Insert) :
String insertSQL = "INSERT INTO yourTable (column1, column2) VALUES ('value1', 'value2')";
int rowsInserted = stmt.executeUpdate(insertSQL);
5.Update :
String updateSQL = "UPDATE yourTable SET column1 = 'newValue' WHERE condition";
int rowsUpdated = stmt.executeUpdate(updateSQL);
6.Delete :
String deleteSQL = "DELETE FROM yourTable WHERE condition";
int rowsDeleted = stmt.executeUpdate(deleteSQL);
7.Select :
String selectSQL = "SELECT * FROM yourTable";
ResultSet rs = stmt.executeQuery(selectSQL);
while (rs.next()) {
// Process each row, e.g.:
System.out.println("Column Value: " + rs.getString("column1"));
}
Cleanup Resources:
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
JDBC is a powerful tool that bridges your Java applications with databases, enabling efficient data manipulation and management. Mastering these steps with Oracle, MySQL, and PostgreSQL can significantly enhance your development workflow.
Feel free to share your experiences and tips with JDBC in the comments!