Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.

Slides:



Advertisements
Similar presentations
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Advertisements

CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular.
Distributed Application Development B. Ramamurthy.
Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Adv. CoE Lab: JDBC 1 Advanced Computer Engineering Lab Objective – –to give some background on JDBC to help with the lab exercises , Semester.
ODBC and JDBC What are they – libraries of function calls that support SQL statements Why do we need them –Provide a way for an application to communicate.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 11. DATABASE CONNECTIVITY IN JAVA JDBC Programming JDBC (Java Database Connectivity) is the specification of a.
Overview 1. What is JDBC? 2. The JDBC-ODBC Bridge 3. JDBC Pseudocode 4. simpJDBC.java.
Lab: JDBC 1 Computer Engineering Lab III v Objective –to give some background on JDBC to help with the lab exercises , Semester 1,
Introduction to JDBC (Java Database Connectivity).
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Programming in Java Corresponds with Chapter 32, 33.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
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.
Computer Engineering Lab
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Presentation On How To Create Connection To A Database.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
JDBC (Java Database Connectivity)
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
SQL and Java The vision for Java is to be the concrete and nails that people use to build this incredible network system that is happening all around us.
Web Programming Assistant Professor Xiaozhong Liu
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
Lec - 14.
JDBC Database Management Database connectivity
HW#4 Making Simple BBS Using JDBC
Interacting with Database
Super Market Management
JAVA DATABaSE CONNECTIVITY
Making Text for the Web part 6
JDBC Example.
Lecture 11 Database Connection
Presentation transcript:

Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006

Georgia Institute of Technology Relational Databases What if you wanted to keep track of the people in your pictures –And more than one person could be in a picture? –You might want one table Person with information about each person –You might want another table Picture with information about each Picture –And another table to PicturePerson to link the two together Which people are in what picture

Georgia Institute of Technology Person Database In your bookClasses directory –Is a Microsoft Access database Person.mdb

Georgia Institute of Technology Where is Jennifer Guzdial? First look up the PersonID for Jennifer Guzdial –She is number 5 Now look in the PicturePerson table for that PersonID to get the ids for the pictures (PictureID) –She is in picture 5 and picture 8 Now use the PictureID to find the file name for that picture –jennySoccer.jpg –MattJennyCorn.jpg

Georgia Institute of Technology Getting Data from a Database Use SQL to get data from the database –Structured Query Language –Works with any relational database Use classes in java.sql –DriverManager – to manage the driver –Connection – to connect to the database –Statement – to send a query to the database –ResultSet – to hold the results of a query

Georgia Institute of Technology Loading the Driver You first have to load the driver class –Handles working with a particular kind of database –Use Class.forName(DriverClass); For an Access Database Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); For a MySQL Database Class.forName(com.mysql.jdbc.Driver);

Georgia Institute of Technology Creating a Connection Specifies the database to connect to –And optionally the login and password to use Examples Connection connection = DriverManager.getConnection(url, "login", "password"); Connection connection = DriverManager.getConnection(url); Specifying the URL –For an Access Database Use an ODBC to JDBC bridge jdbc:odbc:person –For a MySQL Database Use JDBC to MySQL jdbc:mysql://localhost:3306/person

Georgia Institute of Technology Access Database ODBC Name To connect to an Access Database –You will need to assign an ODBC name –Open the Control Panel and then get to the ODBC Datasource Administrator Window In Administrative Tools –Click on the User DSN Tab Click on the Add button

Georgia Institute of Technology Adding the Data Source Name Click on –Diver do Microsoft Access (*.mdb) Click on Finish

Georgia Institute of Technology Mapping a DSN to a Database Add a Data Source Name –person Fill in a description Click on Select and pick the database file –person.mdb

Georgia Institute of Technology Closing a Connection There is often a limit on how many connections there can be to a database at a time –But many programs can be connected at the same time Most database drivers pool the database connections –So you want to close the connection after you finish a query To release it back to the free pool connection.close(); Free pool

Georgia Institute of Technology DatabaseManager import java.sql.*; import java.util.*; /** * Class that handles the connection with the database */ public class DatabaseManager { ////////////// fields //////////////////////////// private String driverName; private String urlStr; /** * Constructor that takes the driver name and url driver the class that communicates with the * database url the url of the database as a string */ public DatabaseManager(String driver, String url) { this.driverName = driver; this.urlStr = url; // try the following try { // load the driver class Class.forName(driver); } catch (ClassNotFoundException ex) { SimpleOutput.showError("Can't find the driver class " + driver + ", check the classpath"); }

Georgia Institute of Technology Testing the Connection /** * Method for testing the connection */ public void testConnection() { // try the following try { // open the connection to the database Connection connection = DriverManager.getConnection(this.urlStr); // tell the user the connection was opened System.out.println("Connection established"); // close the connection connection.close(); // tell the user the connection was closed System.out.println("The connection was closed"); } catch (SQLException ex) { SimpleOutput.showError("Trouble with the " + "database connection"); ex.printStackTrace(); }

Georgia Institute of Technology Main to Test the Connection /* main for testing */ public static void main(String[] args) { // create the database manager for an Access database DatabaseManager dbManager = new DatabaseManager("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:person"); // create the database manager for a MySQL database // new DatabaseManager("com.mysql.jdbc.Driver", // "jdbc:mysql://localhost:3306/person"); // test the connection dbManager.testConnection(); }

Georgia Institute of Technology Getting Data from the Database To query the database –Use a select statement –Specify the fields to be selected –Specify the table to select from Select fieldList From tableName [Where condition] Examples –Select * From Person Will return all rows and fields (in order) in the Person table –Select FirstName, Age From Person Will return all rows with first name and then age –Select * from Person Where Age > 40 Will return rows where the person age is greater than 40

Georgia Institute of Technology Using a Statement Statements are used to send queries to the database You need to create a statement –Only one can be open on a connection at a time –Statement statement = connection.createStatement(); Execute the query –ResultSet rs = statement.executeQuery(query); Process the result set Close the statement –statement.close();

Georgia Institute of Technology Processing the ResultSet The ResultSet is a temporary table of data –We need to walk through each row of data –The cursor starts off before the first row So you can use while (rs.next()) –Get the data for a column using getXXX(int colNum) where XXX is type of data –Starting with 1 as the first column Or getXXX(String colName) –Using the name of the column for colName –Close the result set rs.close();

Georgia Institute of Technology testQuery Method /** * Method to test a query and print the results query the query to execute numCols the number of columns in the result */ public void testQuery(String query, int numCols) { // try the following try { // open the connection to the database Connection connection = DriverManager.getConnection(this.urlStr); // create a statement Statement statement = connection.createStatement(); // execute the query ResultSet rs = statement.executeQuery(query); // print out the results while (rs.next()) { for (int i = 1; i <= numCols; i++) { System.out.print(rs.getString(i) + ", "); } System.out.println(); } // close everything rs.close(); statement.close(); connection.close(); } catch (SQLException ex) { SimpleOutput.showError("Trouble with the database " + urlStr); ex.printStackTrace(); }

Georgia Institute of Technology Main for testQuery /* main for testing */ public static void main(String[] args) { // create the database manager for an Access database DatabaseManager dbManager = new DatabaseManager("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:person"); // create the database manager for a MySQL database // new DatabaseManager("com.mysql.jdbc.Driver", // "jdbc:mysql://localhost:3306/person"); // test a query dbManager.testQuery("Select FirstName, Age from Person",2); }

Georgia Institute of Technology Narrowing the Query /* main for testing */ public static void main(String[] args) { // create the database manager for an Access database DatabaseManager dbManager = new DatabaseManager("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:person"); // create the database manager for a MySQL database // new DatabaseManager("com.mysql.jdbc.Driver", // "jdbc:mysql://localhost:3306/person"); // test a query dbManager.testQuery("Select FirstName, Age from Person " + "Where Age > 40",2); }

Georgia Institute of Technology Exercise Modify the query to get the first name and last name of the people under age 20 Modify the query to get the PictureID of the pictures that have PersonID of 3 in them Modify the query to get the first names of the people with a last name of Guzdial

Georgia Institute of Technology Summary To get data from a Database –You need to load the driver –Know how to connect to the database –Create a connection –Create a statement –Use the statement to execute a query and get a result set Using the SQL Select statement –Process the result set –Close the result set, statement and connection Closing the connection puts it back in the free pool of connecitons