* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download An Introduction to Java Database Connectivity (JDBC)
Microsoft Access wikipedia , lookup
Concurrency control wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Database model wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
CS6401 An Introduction to Java Database Connectivity (JDBC) Tuesday 5 October 2010 RECAP... • Looked at evolution and uses of databases • Introduced n-tier design architecture • Introduced the problem area for our programme Tuesday 5 October 2010 STRUCTURE OF TODAY’S PROGRAM Our Code 1-Tier Program with a single operation (a select) JAVA GENERIC JDBC Vendor JDBC Driver Database Tuesday 5 October 2010 OUR TOOLS TODAY • MySQL Query Browser - available at: http://dev.mysql.com/downloads/guitools/5.0.html but migrating to MySQL Workbench. • MySQL JDBC Driver • Console compilation, Java Runtime Environment (JRE), and Editor (swap in your favorite IDE...) Tuesday 5 October 2010 MYSQL QUERY BROWSER Tuesday 5 October 2010 COMPONENTS OF OUR PROGRAM • Import statements • Connection • Loading • Creating the appropriate driver and creating a connection a statement and executing a query • Processing Tuesday 5 October 2010 String the returned result set IMPORT STATEMENTS import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.Driver; The final one is part of MySQL’s JDBC jar file available at: http://dev.mysql.com/usingmysql/java/ Tuesday 5 October 2010 CONNECTION STRING (13) private final String CONNECTION_STRING = "jdbc:mysql://cosmos.ucc.ie: 3306/database_name?user=db_username&password=db_password"; Need to know: 1. The server and port on which the database management system runs II. The name of the database to which you want to connect to III. The username with which to connect IV. The password for that username Tuesday 5 October 2010 DRIVER AND CONNECTION java.sql.DriverManager.registerDriver(new Driver()); java.sql.Connection con = DriverManager.getConnection(CONNECTION_STRING); 1. We load the appropriate driver - vendor specific code that interfaces with Java’s generic database hander II. We imported the MySQL driver earlier - now we register that as the object Java will talk to in-order to fulfill requests III. We create a connection with the database defined in the connection string Tuesday 5 October 2010 CREATE/EXECUTE A STATEMENT (32,34) java.sql.Statement stmt = con.createStatement(); java.sql.ResultSet rs = stmt.executeQuery("SELECT * FROM photos"); 1. The connection provides us with a statement object that understands how to execute SQL II. We provide the SQL, and the statement takes care of the rest, returning a ResultSet (set of objects representing returned rows from the DB) Tuesday 5 October 2010 PROCESS RESULTS while(rs.next()){ System.out.println(rs.getString(1) + “ “ + rs.getString(2) + “ “ + rs.getString(3) + “ “ rs.getString(4) + “ “ + rs.getString(5) + “\n”); } 1. For each object in the resultset (representing a pointer to a matching row in the table) print out the values of the attributes separated by spaces Tuesday 5 October 2010 PROCESS RESULTS (41-45) while(rs.next()){ System.out.println(rs.getString(“photoid”) + “\n”); } 1. Could also use attribute names to access values, can have any order Tuesday 5 October 2010