Upload
meljun-cortes-mbampa
View
225
Download
0
Embed Size (px)
Citation preview
8/11/2019 MELJUN CORTES Web Application and Database
1/13
JDBC OverviewThe JDBC API makes it possible to do
three things:
Establish a connection with a
database or access any tabular data
source
Send SQL statements
Process the results
8/11/2019 MELJUN CORTES Web Application and Database
2/13
Installing the MySQL
DriverTo use the MySQL Connector/J
Driver, you need to download thecomplete distribution.
Once you have extracted thedistribution archive, you can installthe driver by placing mysql-
connector-java-[version]-bin.jar
in your classpath, either by addingthe FULL path to it to yourCLASSPATH enviornment variable.
To use the driver within Tomcat,copy the jar file above to:
[TOMCAT_HOME]\webapps\ROOT\WEB-INF\lib
8/11/2019 MELJUN CORTES Web Application and Database
3/13
Six Steps to Using JDBC
1. Load the JDBC Driver
2. Establish the Database
Connection
3. Create a Statement Object
4. Execute a Query
5. Process the Results
6. Close the Connection
8/11/2019 MELJUN CORTES Web Application and Database
4/13
1) Loading the JDBC
DriverTo use a JDBC driver, you must load
the driver via the Class.forName()method.
In general, the code looks like this: Class.forName("jdbc.DriverXYZ");
where jbdc.DriverXYZ is the JDBC
Driver you want to load.
If you are using a JDBC-ODBCDriver, your code will look like this: Class.forName("sun.jdbc.odbc.Jdb
cOdbcDriver");
8/11/2019 MELJUN CORTES Web Application and Database
5/13
2) Establish the
ConnectionOnce you have loaded your JDBC
driver, the next step is to establish adatabase connection.
The following line of code illustrates
the basic idea:
Connection con =DriverManager.getConnection(url,
"myLogin", "myPassword");
Example in ODBC String url = "jdbc:odbc:basketball"; Connection con =
DriverManager.getConnection(url,admin", adminpasswd");
Example in MySQL String url =
"jdbc:mysql://localhost/basketball"; Connection con =
DriverManager.getConnection(url);
8/11/2019 MELJUN CORTES Web Application and Database
6/13
3) Create a Statement
ObjectThe JDBC Statement object sends
SQL statements to the database.
Statement objects are created fromactive Connection objects.
For example:
Statement stmt =con.createStatement();
With a Statement object, you canissue SQL calls directly to thedatabase.
8/11/2019 MELJUN CORTES Web Application and Database
7/13
4) Execute a QueryexecuteQuery()
Executes the SQL query and returnsthe data in a table (ResultSet)
The resulting table may be emptybut never null
ResultSet results=statement.executeQuery("SELECT a, b FROM table");
executeUpdate() Used to execute for INSERT, UPDATE,
or DELETE SQL statements The return is the number of rows
that were affected in the database Supports Data Definition Language
(DDL) statements CREATE TABLE,DROP TABLE and ALTER TABLE
8/11/2019 MELJUN CORTES Web Application and Database
8/13
Useful Statement
MethodsgetMaxRows/setMaxRows
Determines the number of rows aResultSet may contain
Unless explicitly set, the number ofrows are unlimited (return value of0)
getQueryTimeout/setQueryTimeout Specifies the amount of a time a
driver will wait for a STATEMENT tocomplete before throwing aSQLException
8/11/2019 MELJUN CORTES Web Application and Database
9/13
5) Process the ResultsA ResultSet contains the results of
the SQL query.
Useful Methods All methods can throw a
SQLException
close
Releases the JDBC and database resources The result set is automatically closed
when the associated Statement object
executes a new query
getMetaDataObject
Returns a ResultSetMetaData objectcontaining information about the columns
in the ResultSet
8/11/2019 MELJUN CORTES Web Application and Database
10/13
5) Process the Results
(Continued) next
Attempts to move to the next rowin the
ResultSet
If successful true is returned;otherwise, false
The first call to next positions the
cursor a the first row
findColumn Returns the corresponding integer value
corresponding to the specified columnname
Column numbers in the result set do notnecessarily map to the same columnnumbers in the database
8/11/2019 MELJUN CORTES Web Application and Database
11/13
ResultSet (Continued) getXxx
Returns the value from the columnspecified by column nameor columnindexas anXxxJava type
Returns 0 or null, if the value is a SQL
NULL Legal getXxxtypes:
Double byte int
Date String float
short long Time Object
8/11/2019 MELJUN CORTES Web Application and Database
12/13
6) Close the ConnectionTo close the database connection:
stmt.close();
connection.close();
Note: Some application servers,maintain a pool of databaseconnections.
This is much more efficient, asapplications do not have theoverhead of constantly opening andclosing database connections.
8/11/2019 MELJUN CORTES Web Application and Database
13/13
Designing a Web
Application Using
DatabaseTo build a database application you
must design the relationship
between the model objects and the
corresponding databaserepresentation.
To design the model elements of an
application, you should perform the
following tasks:
Design the domain objects of your
application
Design the database tables that mapto the domain objects
Design the business services to
separate the database code into
classes