- With JDBC concrete classes come from JDBC Driver. Each database has its own jar
- Interfaces, all interfaces are in java.sql
–> Driver : Establishes connection to the database
–> Connection : Sends command to the database
–> PreparedStatement : Executes SQL Query
–> CallableStatement : Executes command stored in database
–> ResultSet : Reads the results of the query - All JDBC classes are in java.sql module
4.JDBC URL separated by colon –> protocol:subprotocol:subname, protocol is always “jdbc”, protocol sub name is name of the database. subname can have any syntax depends on the database vendor. - DriverManager.getConnection throws checked SQLException
–> DriverManager.getConnection(“connection string”)
–> DriverManager.getConnection(“connection stirng”, “username”, “password”) - Statement is interface
PreparedStatement is also an interface extending Statement A Statement and a PreparedStatement are similar to each other, except that a PreparedStatement takes parameters, while a Statement does not.
ps = conn.preparedStatement(“update/insert/deleted sql query”). ps.executeUpdate() returns int, number of rows updated/inserted/deleted
ps = conn.preparedStatement(“select sql query”). ps.executeQuery() returns ResultSet
ps = conn.preparedStatement(“any query, select or update query”). ps.execute() returns boolean. if it returns true, ps.getResultSet() that means it was select query. if it returns false then use ps.getUpdateCount() returns a int. CallableStatementis also an interface extending Statement
cs.executeQuery() returns a result set
cs.execute() returns the OUT parameter if you pass “select query” to executeUpdaet() you get SQLException, statement does not generate a row count. - bind variables in sql query start to set parameter from 1 and not 0
if you don’t set at least 1 bind variable, it give s exception and if you try to set more than needed bind variable again exception - Attempting to access a column name or index that does not exist throws a SQLException, as does getting data from a ResultSet when it isn’t pointing at a valid row