intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: 5A4F5AFSDG 5A4F5AFSDG | Ngày: | Loại File: PDF | Số trang:72

52
lượt xem
4
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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.

Chủ đề:
Lưu

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

  1. Bài 5 : Java – JDBC Java Web Application JavaServer Pages GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 5 - 1/72
  2. 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
  3. 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, visit http://docs.oracle.com/javase/tutorial/jdbc/overview/index.html Đoàn Thiện Ngân Bài 5 - 3/72
  4. 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
  5. JDBC Product Components JDBC 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
  6. JDBC Product Components 2. 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
  7. JDBC Product Components 3. 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
  8. 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
  9. 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
  10. java.sql Chú ý các đối tượng cơ sở dữ liệu trong java.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
  11. JDBC in Java – DisplayActor.java import 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/72
  12. try { 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
  13. JDBC in Java for (int i = 1; i
  14. JDBC in Java finally { // 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
  15. Install MySQL JDBC • Download MySQL JDBC http://dev.mysql.com/downloads/connector/j mysql-connector-java-5.1.33-bin.jar • Install MySQL JDBC into JRE directory: \lib\ext\ • Netbeans 8.0.1 ‘s MySQL JDBC \ide\modules\ext\ javac DisplayActor.java java DisplayActor Đoàn Thiện Ngân Bài 5 - 15/72
  16. MySQL JDBC Driver - Netbeans • MySQL JDBC: MySQL Connector/J Driver • Netbeans hỗ trợ kết nối JDBC rất tốt. – Nhấp vào Services – Có 2 cách tạo connection để test JDBC với MySQL trước khi dùng 1. Dùng ngay thẻ Drivers, chọn MySQL Connector/J Driver, R-click chọn Connect Using… 2. Dùng ngay thẻ Database, R-click chọn New Connection, cửa sổ New Connection Wizard, chọn MySQL Connector/J Driver, … Đoàn Thiện Ngân Bài 5 - 16/72
  17. MySQL Connector/J Driver • Netbeans • Services/ Databases/ Drivers/ MySQL(…) Đoàn Thiện Ngân Bài 5 - 17/72
  18. MySQL Connector/J Driver • R-Click MySQL (. )/ Connect Using … Đoàn Thiện Ngân Bài 5 - 18/72
  19. MySQL Connector/J Driver • Host • Port • Database • User Name • Password • Remember password • Test Connection Đoàn Thiện Ngân Bài 5 - 19/72
  20. MySQL Connector/J Driver Đoàn Thiện Ngân Bài 5 - 20/72
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2