Implementing Java Database Connectivity JDBC

J.E.D.I

5.5 Implementing Java Database Connectivity JDBC

Most applications use relational database system as the repository of data. A language called Structured Query Language SQL is almost universally used among these systems. They are used to make queries, i.e., request information that satisfy certain criteria. Java enables programmer to write code that uses SQL queries to access the information in a relational database systems. The engineering of the database that implements the data of the application is beyond the scope of this course and is normally discussed in a database course. This section discusses how Java uses JDBC to connect and request services to a database server. In the example, we will show a fraction of the code that retrieves athlete records in the database. The code assumes that Athlete and PCLAthlete have been implemented already. The fraction of codes shown in Text 11 and Text 12 is an implementation of the DBAthlete Read wherein the PCLAthlete is populated with athlete records that satisfy a criteria based on user input. The import java.sql statement imports the package that contains classes and interfaces for manipulating relational databases in Java. The constructor of the class attempts to connect to the database. If successful, queries can be made to the database. The following lines specify the database URL Uniform Resource Locator that helps the program locate the database, username and password. The URL specifies the protocol for communication jdbc, the subprotocol odbc and the name of the database ABLDatabase. The username and password are also specified for logging into the database; in the example, the username is postgre and the password is postgres1. String url = “jdbc:odbc:ABLDatabase”; String username = “postgre”; String password = “postres1”; The class definition for the database driver must be loaded before the program can connect to the database. The following line loads the driver. In this example, the default JDBC-to-ODBC-bridge database driver is used to allow any Java program to access any ODBC data source. For more information on JDBC driver and supported databases visit the following site: http:java.sun.comproductsjdbc. Class.forName“sun.jdbc.odbc.JdbcOdbcDriver”; To establish a connection, a connection object is created. It manages the connection between the database and the Java program. It also provides support for executing SQL statements. In the example, the following statement provides this connection. connection = DriverManager.getConnectionurl, username, password; The initial method that is invoke to start the retrieval of athlete records to the database is the getAthleteRecord method. The code of this method is shown below. The string Software Engineering 217 J.E.D.I Software Engineering 218 import java.sql.; import java.util.; public class DBAthlete { private Connection connection; private PCLAthlete athleteList; public DBTeam{ To connect to the database... String url = “jdbc:odbc:ABLDatabase”; String username = “postgre”; String password = “postgres1”; try { Class.forName“sun.jdbc.odbc.JdbcOdbcDriver”; connection = DriverManager.getConnection url, username, password; } catch ClassNotFoundException { . . . } } .... to execute SELECT statement public void getAthleteRecordString searchCriteria { Statement statement; ResultSet rs; String selectStmt; selectStmt = prepareSelectStmtsearchCriteria; statement = connection.createStatement; rs = statement.executeQueryselectStmt; populatePCLAthlete rs ; statement.close; } to generate the SELECT-statement with the search criteria private String prepareSelectStmtString searchCriteria { String query = SELECT FROM athlete, guardian query = query + WHERE athlete.guardianID = guardian.guardianID; query = query + AND + searchCriteria + ; ; return query; } Text 11 DBAthlete Implementation J.E.D.I selectStmt is used to define the select-statement that specifies what records are retrieved from the database. The private method prepareSelectStmt generates the select-statement with the appropriate criteria. The statement object is used to query the database and is instantiated using the createStatement method of the connection object. The executeQuery method of the statement object is used to execute the Select-statement as specified in selectStmt string. The result of the query is placed and referenced in rs which is a ResultSet object. The PCLAthlete object is populated by executing the populatePCLAthlete method which takes as input the ResultSet. For each record in the result set, an athlete object is instantiated and the data of the athlete are assigned to the attributes of this object. The getPCLAthlete method returns a reference to the generated athlete list. Software Engineering 219 to populate the persistent class list. private void populatePCLAthleteResultSet rs{ .... athleteList = new PCLAthlete; .... inside a try and catch block ResultSetMetaData rsmd = rs.getMetaData; do { athleteList.add getNextMember rs, rsmd ; } while rs.next } to generate an athlete object to be added to the PCLAthlete private Athlete getNextMember ResultSet rs, ResultSetMetaData rsmd{ Athlete a = new Athlete; a.setAthleteID rs.getString 1 ; assumes member has a constructor to convert string to integer a.setLastName rs.getString2 ; a.setFirstName rs.getString3 ; ... set attributes to the athlete object a return a; } return a reference to the athlete list PCLAthlete public PCLAthlete getPCLAthlete { return athleteList; } } Text 12 DBAthlete Implementation Continuation J.E.D.I

5.6 Implementing the Graphical User Interface