Presentation is loading. Please wait.

Presentation is loading. Please wait.

JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.

Similar presentations


Presentation on theme: "JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize."— Presentation transcript:

1 JDBC Java and Databases, including Postgress

2 JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize on existing DB APIs –JDBC should be simple

3 SQL level l you should be able to construct SQL statements and embed them in Java API calls l jdbc vs odbc: C based and overly complex l jdbc portability is a big asset

4 Developing JDBC Applications l Import the jdbc classes l load the jdbc driver l Connect to the database l Submit queries, inserts, updates and process responses Don’t forget to commit any changes! l Disconnect from the database

5 Import the jdbc files l import java.sql.*; will import all of the sql classes for jdbc This will include Drivers, Driver Managers, Connections, Statements, ResultSets, etc.

6 Load the Driver l PC: new JdbcOdbcDriver(); l Others: try // load driver { Class.forName( “string name of driver” ); } catch ( Exception e ) { e.printStackTrace(); return; }

7 Postgress l Driver available at URL –http://www.postgresql.org/ftp/http://www.postgresql.org/ftp/ –Install, then download JDBC JAR file at: http://jdbc.postgresql.org/download.html and place it in your classpath (including the jar name) try // load driver { Class.forName(“ org.postgresql.Driver ” ); } catch ( Exception e ) { e.printStackTrace(); return; }

8 The Connection Interface l represents a session with a specific DB l all SQL statements executed and results returned within the context of the connection l A Java application can have any number of connections to one or more DBs l through the connection we have access to the DB and its stored information, including metadata l autocommit is the default – be aware of transactions. Turn off autocommit and commit explicitly. l use DriverManager.getConnection(url) to create a connection l See Java Doc on Connection

9 Connect to Database try {connection = DriverManager.getConnection (url,usercode,password); System.out.println(“Connected to Database”); // do some db stuff here } catch ( SQLException e ) { e.printStackTrace(); } Postgress URL would start with: jdbc:postgresql:// followed by database. last two arguments are database login: user, password

10 Database Interaction l All information henceforth is common to all databases. JDBC handles the different databases transparently –Statement used for SQL statements without parameters –PreparedStatement same statement with different explict data values. Precompiled for efficiency with most drivers –CallableStatement used for executing stored functions or procedures –See Java Doc on each of the above

11 Statement l connection.createStatement() creates a statement for SQL. Statement queryStmt = conn.createStatement(); l statement.executeQuery(SQLstatemen t as a string); ResultSet result = queryStmt.executeQuery(queryStr);

12 PeparedStatement l connection.prepareStatement(SQL statement as a String with parameters); PreparedStatement q = conn.prepareStatement(qString); l // set parameters then execute statement l statement.execute(); ResultSet result = q.executeQuery(); l See PrepStatement.java

13 CallableStatement l connection.prepareCall(“String representing call to a stored procedure/function”); CallableStatement verify = conn.prepareCall( "{ ? = call gord7932.verifyLogin(?,?,?,?,?,?) }" ); l The ? represents parameter in numeric order starting at 1 l See javaCallable.java

14 ResultSet l public abstract boolean next() l public abstract void close() l public abstract boolean wasNULL() l public abstract XXX getXXX( int column) public abstract XXX getXXX(String columnname) where XXX is a type like String, Int, etc. l ResultSet may be scrollable (navigated in both directions) l See Java Doc and the previous examples

15 More Connection Methods l public abstract void close() l public abstract void setAutoCommit(boolean val) l public abstract void commit() l public abstract void rollback() l each of these methods throws SQLException

16 Insert, Delete, Update l use executeUpdate method in either a Statement, PreparedStatement, or CallableStatement


Download ppt "JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize."

Similar presentations


Ads by Google