Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.

Similar presentations


Presentation on theme: "CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC."— Presentation transcript:

1 CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC

2 CSC411_L12_JDBC_MySQLDr. Nan Wang 2 Web-based database applications Online book store What are needed: –Web pages –Web server Apache –Database –Database server MySQL –CGI scripts Java serverlet –Connector which enables applications connect to database JDBC 2

3 CSC411_L12_JDBC_MySQLDr. Nan Wang 3 Web Server 3

4 CSC411_L12_JDBC_MySQLDr. Nan Wang 4 Database Server 4

5 CSC411_L12_JDBC_MySQLDr. Nan Wang 55 Introduction RDBMS –Relational database management system –MySQL Open source Available for both Windows and Linux dev.mysql.com/downloads/mysql/5.0.hml JDBC –Java Database Connectivity –JDBC driver Enable Java applications to connect to database Enable programmers to manipulate databases using JDBC

6 CSC411_L12_JDBC_MySQLDr. Nan Wang 66 Instructions to Install MySQL and MySQL Connector/J Install MySQL –Insert CD and change directory to D:\software\MySQL\mysql-4.0.20c-win –Double click SETUP.EXE –Following the instruction Install MySQL Connector/J –Copy mysql-connector-java-3.0.14-production.zip –Open mysql-connector-java-3.0.14-production.zip Extract its content to the C:\ driv

7 CSC411_L12_JDBC_MySQLDr. Nan Wang 77 Instructions on Setting MySQL User Account Set up a user account –Start database server by executing the script C:\mysql\bin\mysqld –Start the MySQL monitor by executing the command C:\mysql\bin>mysql –h localhost –u root –Create an account mysql> USE mysql; mysql> INSERT INTO user SET Host=‘localhost’, User=‘jhtp6’, Password=PASSWORD(‘jhtp6’), Select_priv=‘Y’, Insert_priv=‘Y’, Update_priv=‘Y’, Delete_priv=‘Y’, Create_priv=‘Y’, Drop_priv=‘Y’, References_priv=‘Y’, Execute_priv=‘Y’; mysql> FLUSH PRIVILEGES; mysql> exit;

8 CSC411_L12_JDBC_MySQLDr. Nan Wang 88 Creating Database books in MySQL Create books database –Open Command Prompt –Change to the C:\mysql\bin directory –Start database by executing the command C:\mysql\bin\mysqld –Copy SQL script books.sql to C:\mysql\bin directory –Open another Command Prompt –Change to the C:\mysql\bin directory –Create the books database by executing the command C:\mysql\bin>mysql –h localhost –u jhtp6 –p < books.sql

9 CSC411_L12_JDBC_MySQLDr. Nan Wang 99 Relational Databases Relational database –Table Rows, columns –Primary key Unique data SQL queries –Specify which data to select from a table Employee table

10 CSC411_L12_JDBC_MySQLDr. Nan Wang 10 Relational Database Overview: The books Database Sample books database –Four tables authors –authorID, firstName, lastName publishers –publisherID, publisherName titles –isbn, title, editionNumber, copyright, publisherID, imageFile, price authorISBN –authorID, isbn

11 CSC411_L12_JDBC_MySQLDr. Nan Wang 11 Sample Data ( author and publisher )

12 CSC411_L12_JDBC_MySQLDr. Nan Wang 12 Sample data ( titles and authorISBN)

13 CSC411_L12_JDBC_MySQLDr. Nan Wang 13 Table relationships in books

14 CSC411_L12_JDBC_MySQLDr. Nan Wang 14 Manipulating Databases with JDBC Seven Basic Steps in Using JDBC 1.Load the driver 2.Define the Connection URL 3.Establish the Connection 4.Create a Statement object 5.Execute a query 6.Process the results 7.Close the connection

15 CSC411_L12_JDBC_MySQLDr. Nan Wang 15 JDBC Drivers JDBC consists of two parts: –JDBC API, a purely Java-based API –JDBC Driver Manager,which communicates with vendor-specific drivers that perform the real communication with the database. Point: translation to vendor format is performed on the client –No changes needed to server –Driver (translator) needed on client

16 CSC411_L12_JDBC_MySQLDr. Nan Wang 16 Connecting to and Querying a Database DisplayAuthors –Retrieves the entire authors table –Displays the data in the standard output stream –Example illustrates Connect to the database Query the database Process the result http://www.apl.jhu.edu/~hall/java/Servlet- Tutorial/Servlet-Tutorial-Intro.htmlhttp://www.apl.jhu.edu/~hall/java/Servlet- Tutorial/Servlet-Tutorial-Intro.html

17 CSC411_L12_JDBC_MySQLDr. Nan Wang 17 Example: DisplayAuthors.java (1) Imports the JDBC classes and interfaces from package java.sql.* Specifies the JDBC driver’s class name Specifies the database URL Loads the database driver. Declare and initialize a Connection reference

18 CSC411_L12_JDBC_MySQLDr. Nan Wang 18 Example: DisplayAuthors.java (2) Obtain an object that implements interface Statement. execute a queryObtains the metadata.Retrieve the number of columns Obtain column name Position to the first row in the ResultSet with method next Extract the contents of one column in the current row Catch SQLException

19 CSC411_L12_JDBC_MySQLDr. Nan Wang 19 Example: DisplayAuthors.java (3) Close the Statement and the database Connection. ClassNotFoundException is thrown if the class loader cannot locate the driver class

20 CSC411_L12_JDBC_MySQLDr. Nan Wang 20 Popular JDBC driver names and database URL. For more information on JDBC drivers, visit the Sun Microsystems JDBC Web site, http://developers.sun.com/product/jdbc/drivers

21 CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 21 Questions?


Download ppt "CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC."

Similar presentations


Ads by Google