Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P5

Số trang: 10      Loại file: pdf      Dung lượng: 311.31 KB      Lượt xem: 5      Lượt tải: 0    
tailieu_vip

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P5:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P5 Figure 4.5: The Type 3 JDBC-Net driver. Type 4: Native-Protocol, Pure Java Driver The Type 4 drivers are pure Java drivers that communicate directly with the vendors database. They do this by converting JDBC commands directly into the database engines native protocol. The Type 4 driver has a very distinct advantage over all the other driver types. It has no additional translation or middleware layers, which improves performance tremendously. Figure 4.6 diagrams the communications of a Type 4 driver. Figure 4.6: The Type 4 native-protocol JDBC driver. JDBC Basics Now that we have discussed what the JDBC is and some of its characteristics, lets start learning how to use it. In this section, we are going to discuss how to install and set up a Type 1 driver, make a connection to the database, and perform the basic SQL commands. Installing and Setting Up a Type 1 Driver In the JDBC examples throughout this book, we will be connecting to a Microsoft Access database using a Type 1 driver. To install the JDBC-ODBC Bridge, simply download the JDK and follow the installation directions. The JDBC-ODBC Bridge is included in version 1.1 and later. The JDBC-ODBC Bridge requires no specific setup steps, but the ODBC driver does. For our examples, assume that you are using a PC and running Windows 9x or NT. If not, you will need to create your own database using the drivers supplied by your database vendor. To configure the ODBC data source for the examples, you will follow these steps:1. Copy to your local drive the database file moviecatalog.mdb, which can be found on this books Web site.2. Start the application ODBC Administrator found in the Windows Control Panel. You will see a window similar to the one in Figure 4.7. - 41 - Figure 4.7: The ODBC Administrator.3. Select the Add button to add a new data source. Figure 4.8 shows the Create New Data Source screen. Figure 4.8: The Create New Data Source screen.4. Select the Microsoft Access Driver and click the Finish button. You will now be presented with the ODBC Microsoft Access Setup screen. Figure 4.9 shows this screen. Figure 4.9: The ODBC Microsoft Access Setup screen.5. Enter the string Movie Catalog as the data source name and click the database Select button. Enter the path to the location of your moviecatalog.mdb file and click OK. You will now see the ODBC Microsoft Access Setup screen with your changes displayed. Click OK to commit to your changes.6. You will now see the ODBC Data Source Administrator screen with the Movie Catalog data source in the list. Click the OK button to close this window. Establishing a Database Connection The first thing you need to do when using the JDBC is establish a connection to the database. This is a two-step process. You must load the JDBC driver and then make a connection. Loading a JDBC driver is very simple. It takes only one line of code. In our examples, we will be using the JDBC-ODBC Bridge. The class name for this driver is sun.jdbc.odbc.JdbcOdbcDriver. The following code snippet shows you how to load this driver: Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); When you call the Class.forName() method, it creates an instance of the driver and registers it with the DriverManager. This is all there is to loading a JDBC driver. After you have the driver loaded, it is easy to make a connection. You make a call to the static method DriverManager.getConnection(), which returns a connection to the database. Its method signature is listed as follows: - 42 - public static synchronized Connection getConnection(String url, String user, String password) throws SQLException The first parameter is the URL that points to our data source. In the case of the JDBC ODBC Bridge, it always begins with jdbc:odbc:DataSourceName, where the DataSourceName is the name of the ODBC data source you set up. The next two parameters are self-explanatory. They are the username and password associated with the database login. For our example, we will use empty strings for each. Here is the code used to open a connection to our Movie Catalog database: con = DriverManager.getConnection(jdbc:odbc:Movie Catalog, , ); The Connection object returned from the DriverManager is an open connection to the database. We will be using it to create JDBC statements that pass SQL statements to the database. Performing the Basic SQL Commands In this section we are going to look at how to create a JDBC Statement ...

Tài liệu được xem nhiều: