Please enable JavaScript.
Coggle requires JavaScript to display documents.
JDBC API (CRUD commands (Create table:
Statement st = conn…
JDBC API
CRUD commands
-
Insert into some table or update a table:
Statement st = conn.createStatement();
st.executeUpdate( "insert into...");
Select from a table:
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select... from... where...")
Delete Rows:
String sql = "delete from ... where ...";
PreparedStatement delSt = conn.prepareStatement();
int affectedRows = delSt.executeUpdate(sql);
-
Summary
- Allows Java applications to interact with databases. It supports executing SQL statements and handling results from different data sources.
- Java applications connect to a database via a driver. JDBC requires a driver for every database.
- SQL statements are sent directly to the database server every time. PreparedStatement can be used where one statement will be executed multiple times.
-
Basic Process
- Create and establish a connection to some database. Either via the class java.sql.DriverManager or by using the interface javax.sql.DataSource
e.g. DriverManager.getConnection(...)
- Through java.sql.Connection CRUD SQL statements can be executed
- The above statements can be created via java.sql.Statement and java.sql.PreparedStatement. Second one is used when one statement has to be executed multiple times.
Null Values
-
Solution: A good practice when dealing with SQL null values is avoiding primitive types. The method wasNull() from the class ResultSet is useful in this scenario.
-
-
Transactions
-
Usage
- Connection.setAutoCommit(...) should be set to false
- Connection.rollback() undoes all changes made in the previous transaction
- Connection.commit() persists all changes since the last commit/rollback into the database
Connection Pools
The way JDBC manages connections. A connection is borrowed from the pool, used and then returned back. There are different implementations available. More here.