Danh mục tài liệu

Bài giảng Phát triển ứng dụng nguồn mở: Bài 5 - Đoàn Thiện Ngân

Số trang: 72      Loại file: pdf      Dung lượng: 639.97 KB      Lượt xem: 17      Lượt tải: 0    
Xem trước 8 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Bài 5: Java – JDBC java web application javaserver pages. Bài giảng này gồm có những nội dung chính sau: Tổng quan về JDBC, kết nối cơ sở dữ liệu MySQL, kết nối cơ sở dữ liệu PostGreSQL, JSP – JavaServer page, apache tomcat server, glassfish server, JDBC resource, JDBC connection pool. Mời các bạn cùng tham khảo.
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển ứng dụng nguồn mở: Bài 5 - Đoàn Thiện Ngân Bài 5 : Java – JDBCJava Web Application JavaServer Pages GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 5 - 1/72 Nội dung• Tổng quan về JDBC.• Kết nối cơ sở dữ liệu MySQL.• Kết nối cơ sở dữ liệu PostGreSQL.• JSP – JavaServer Page.• Apache Tomcat Server• Glassfish Server• JDBC Resource• JDBC connection pool Đoàn Thiện Ngân Bài 5 - 2/72 Tổng quan về JDBC• Java programs communicate with databases and manipulate their data using the JDBC™API (Java Database Connectivity).• A JDBC driver enables Java applications to connect to a database in a particular DBMS and allows programmers to manipulate that database using the JDBC API.• JDBC is almost always used with a relational database. However, it can be used with any table-based data source.• For more information on JDBC, visithttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.html Đoàn Thiện Ngân Bài 5 - 3/72 JDBC API• The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database.• JDBC helps you to write java applications that manage these three programming activities: – Connect to a data source, like a database – Send queries and update statements to the database – Retrieve and process the results received from the database in answer to your query Đoàn Thiện Ngân Bài 5 - 4/72 JDBC Product ComponentsJDBC includes four components:1. JDBC API: provides programmatic access to relational data. Applications can execute SQL statements, retrieve results, and propagate changes back to an underlying data source. It can also interact with multiple data sources in a distributed, heterogeneous environment. The JDBC 4.0 API is divided into two packages: java.sql and javax.sql. Both packages are included in the Java SE and Java EE platforms. Đoàn Thiện Ngân Bài 5 - 5/72 JDBC Product Components2. JDBC Driver Manager: defines objects which can connect Java applications to a JDBC driver. DriverManager has traditionally been the backbone of the JDBC architecture. It is quite small and simple. The Standard Extension packages javax.naming and javax.sql let you use a DataSource object registered with a Java Naming and Directory Interface™ (JNDI) naming service to establish a connection with a data source. You can use either connecting mechanism, but using a DataSource object is recommended whenever possible. Đoàn Thiện Ngân Bài 5 - 6/72 JDBC Product Components3. JDBC Test Suite: The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These tests are not comprehensive or exhaustive, but they do exercise many of the important features in the JDBC API.4. JDBC-ODBC Bridge: provides JDBC access via ODBC drivers. We need to load ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a three-tier architecture. Đoàn Thiện Ngân Bài 5 - 7/72 JDBC Architecture – 2 tier• JDBC API supports both two-tier and three- tier processing models for database access. Đoàn Thiện Ngân Bài 5 - 8/72 JDBC Architecture – 3 tier• Commands are sent to a middle tier of services, that then sends the commands to the data source. Đoàn Thiện Ngân Bài 5 - 9/72 java.sqlChú ý các đối tượng cơ sở dữ liệu trongjava.sql• import java.sql.Connection;• import java.sql.DriverManager;• import java.sql.ResultSet;• import java.sql.ResultSetMetaData;• import java.sql.SQLException;• import java.sql.Statement;Ví dụ lấy dữ liệu từ table Actor trong cơ sở dữliệu sakila của MySQL - manually coding. Đoàn Thiện Ngân Bài 5 - 10/72 JDBC in Java – DisplayActor.javaimport java.sql.Connection;import java.sql.Statement;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;public class DisplayActor { static final String DB_URL = jdbc:mysql://localhost/sakila; static final String DB_USER = root; static final String DB_PWD = secret;public static void main(String args[]) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; Đoàn Thiện Ngân Bài 5 - 11/72try { JDBC in Java // establish connection to database connection = DriverManager.getConnection( DB_URL, DB_USER, DB_PWD); // create Statement for querying database statement = connection.createStatement(); // query database resultSet = statement.executeQuery( SELECT * FROM actor); // process query results ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); System.out.println(Actor Table Database:\n); Đoàn Thiện Ngân Bài 5 - 12/72 JDBC in Java for (int i = 1; i JDBC in Javafinally { // resultSet, connection , … are closed try { resultSet.close(); statement.close(); connection.close(); } // end try catch (Exception exp) { exp.printStackTrace(); } // end catch } // end finally } // end main} // end class Đoàn Thiện Ngân Bài 5 - 14/72 Install MySQL JDBC• D ...

Tài liệu có liên quan: