– Platform independent, Database independen • Wide range of data sources possible: – SQL, spreadsheets, flat files • JDBC API – Estabish a connection to database – Execute SQL – Process the results
• To code in Java is always the same, changing the driver changes the connection to different database. • Lot’s of drivers for different databases: MySQL, Text, MS SQL Server..
– Type 1 that calls native code of the locally available ODBC driver. – Type 2 that calls database vendor native library on a client side. – Type 3, the pure-java driver that talks with the server-side middleware that then talks to database. – Type 4, the pure-java driver that uses database native protocol. • In most cases you can access the same database with four different type of drivers • List of drivers: – http://developers.sun.com/product/jdbc/drivers
only, no updatable result sets • JDBC 2.0 – Scrollable an updatable result sets • JDBC 3.0 was included in J2SE 1.4 – More metadata, named parameters in callable statements, some new datatypes • JDBC 4.0, Java SE 6 – Easier driver loading, RowSet for offline use, XML-support – JDBC 4.1, Java SE 7 – JDBC 4.2, Java SE 8
// 1. Register driver. Driver String is given to you by the driver // documentation. Driver (.jar) must be in classpath! Class.forName("com.mysql.jdbc.Driver"); // 2. Connect to database. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "username", "password"); // 3. Some SQL Magic. Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM Clients"); // 4. Handle Result while (rs.next()) { result += rs.getString("Firstname") + "<br>"; } // 5. Close the connection rs.close(); statement.close(); conn.close(); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } return result; }
Apache – Really small, only 2.6 MB for base engine and JDBC driver – Supports ANSI/ISO SQL – Supports JDBC, ODBC, Perl, PHP, CLI • Oracle distributes Apache Derby under the name of Java DB. It's included in JDK and in Glassfish! • ij – app for giving SQL commands to database
same process than the "client" – Database files are locked to the client – Only one app accessing the data • 2) Network mode – Database runs in it's own process – Several clients can access the database over the network
• Go to this working directory and give command ij • Create database in the working directory – CONNECT 'jdbc:derby:mydatabase;create=true'; • Give SQL commands – CREATE TABLE CUSTOMERS (....); – INSERT INTO CUSTOMERS VALUES (...); • And exit when ready – exit;
Derby Driver (jar) is not by default in classpath • Windows – java -cp .;%DERBY_HOME%\lib\derby.jar DerbyJdbc • Mac OS X – java -cp .:$DERBY_HOME/lib/derby.jar DerbyJdbc
• java -jar %DERBY_HOME%\lib\derbyrun.jar server start – Mac • java -jar $DERBY_HOME/lib/derbyrun.jar server start • Notice that the directory where you start your server is important. – All the databases are saved to the dir where you started the network server • Once your network server runs, open another terminal window and start ij and connect & create database – CONNECT 'jdbc:derby://localhost:1527/seconddb;create=true'
connection url • jdbc:derby://localhost:1527/seconddb;create=true – When running, use different jar in classpath – Windows • java -cp .;%DERBY_HOME%\lib\derbyclient.jar DerbyJdbc – Mac OS X • java -cp .:$DERBY_HOME/lib/derby.jar DerbyJdbc
feature used to execute the same or similar database statements repeatedly with high efficiency • Simple way to protect against SQL injection String sql = "UPDATE table1 set one = ?, two = ?"; PreparedStatement preStmt = con.prepareStatement(sql); preStmt.setInt(1, 123); preStmt.setString(2, "myNewValue2"); preStmt.executeUpdate();