Databases Flashcards
jdbc url
+ oracle thin
+ derby
jdbc:provider:driverType:connectionDetails
jdbc:oracle:thin:@host:port:dbName
jdbc:derby:host:port:dbName
Obyc. update statement
Syntax od zacatku do konce
Connection conn = DriverManager.getConnection(url, user, pass);
Statement s = conn.createStatement(“update …”);
int updatedRows = s.executeUpdate();
Tvorba prepared statementu se 2 String parametry - dva zpusoby setovani.
PreparedStatement ps = conn.prepareStatement(“select … ?”);
ps.setObject(1, “value”, Types.VARCHAR);
ps.setString(2, “value);
Tvorba callable statementu s in i out parametrem numerickym
CallableStatement cs = conn.prepareCall(“? = function(?)”);
cs.registerOutParameter(1, Types.NUMERIC);
cs.setBigDecimal(2, value)
metody vykonani statementu (3)
+ jak jinak ziskat vysledek a pocet zmenenych radku (2)
ResultSet rs = st.executeQuery();
int rows = st.executeUpdate();
boolean isSelect = st.execute();
st.getResultSet()
st.getUpdateCount()
Auto-commit chovani
+ jak ho zjistit a ovlivnit
Default: auto-commit po exekuci statementu
conn.getAutoCommit()
conn.setAutoCommit(false)
metody ridici transakce (2,5)
conn.commit()
conn.rollback()
Savepoint sp = conn.setSavepoint()
conn.rollback(sp)
Zavirani jdbc
Nutne vzdy!
try with resources
nebo finally
ResultSet - typy (3)
pohyby (6)
ResultSet.TYPE_FORWARD_ONLY - jen next()
ResultSet.TYPE_SCROLL_INSENSITIVE - zmeny se neprojevi
ResultSet.TYPE_SCROLL_SENSITIVE
next(), previous(), first(), last(), absolute(rowNumber), relative(rows)
ResultSet - concurrency (2)
= updatovatelnost
ResultSet.CONCUR_READ_ONLY
ResultSet.CONCUR_UPDATABLE
ResultSet - holdability (2)
= dostupnost dat po commitu
ResultSet.HOLD_CURSOR_OVER_COMMIT
ResultSet.CLOSE_CURSOR_AT_COMMIT
ResultSet - jak ziskat pocet sloupcu, jmeno a typ 1. sloupce
ResultSetMetaData rsmd = ResultSet.getMetaData()
rsmd.getColumnCount()
rsmd.getColumnName(1)
rsmd.getColumnType(1)
Databazova vyjimka - typ a dve metody vlastnosti
SQLException
e.getErrorCode
e.getSQLStatus
Ziskani date hodnoty z ResultSet (2)
if (rs.next()) {
Date x = rs.getObject(1, Date.class);
// Date x = rs.getDate(1);
}
Jak zjistit, jestli db podporuje outer join nebo savepoint?
DatabaseMetaData dbmd = conn.getDatabaseMetaData();
dbmd.supportsOuterJoins()
dbmd.supportsSavepoints()