Você está na página 1de 6

Creating Database Server Types and Data Sources

April 2006 [Revision number: V2.1-1]


Copyright 2006 Sun Microsystems, Inc.

This tutorial walks you through the process of adding a database server type and a data source in the Sun Java Studio Creator integrated development environment (the IDE). You enable the IDE to use database software for which the IDE does not already have drivers. You also add a database to be used as a data source by your applications. Contents - About Database Servers - Creating a Database Server Type - Creating a Data Source

About Database Servers


To create a database connection from the IDE, you must use a driver that is registered in the IDE and a driver that supports the JDBC API (the JDBC driver) 3.0. This tutorial uses the MySQL driver, but you can use any JDBC 3.0 compliant driver. The following servers are registered when you install the IDE:
q q q q q

IBM DB2 Oracle Bundled Database Microsoft SQL Server Sybase

If you already use one of these registered database servers, go to the Creating a Data Source section of this tutorial.

Creating a Database Server Type


To add a database server type to the IDE, download the appropriate JDBC driver and install it if needed. Ensure that the database server is runningwhile adding a data source for this server type, you attempt to connect to the database server. To add MySQL as a registered driver in the IDE, follow these steps. 1. Download the MySQL JDBC driver . You need at least version 3.1.11 of the JDBC driver. 2. Extract the files into a folder on your system. Note: Do not use spaces in your folder path name. 3. In the Servers window in the IDE, right-click Data Sources and choose Edit Database Server Types from the popup menu, as shown in the following figure.

Figure 1: Edit Database Server Types 4. Click New in the Edit Database Server Types dialog box. 5. Navigate to the directory into which you extracted the MySQL JDBC driver. 6. Select the mysql-connector-java-driverversion-bin.jar file and click Open. The IDE creates a database server type named ServerType1. 7. Rename the database server type by entering MySQL in the Display Name text field. 8. Consult your JDBC vendor documentation for the correct JDBC driver class name and database URL template. Click Suggest to get the possible driver classes and the URL template for the specified JDBC driver JAR files.
r

Driver Class Name. For MySQL, the suggested driver class is org.gjt.mm.mysql.Driver or com.mysql.jdbc.Driver (see Figure 2). If you click the Suggest button, and your database server has multiple driver classes available, select the appropriate driver in the Driver Class Name box. Some suggested driver names might not work, so you might need to try more than one. URL Template. The suggested URL template for MySQL is jdbc:mysql://#HOSTNAME/ #DATABASE (see Figure 2). #HOSTNAME and #DATABASE are IDE-specific parameters. When you create a data source using this database server type, the IDE automatically replaces #HOSTNAME and #DATABASE with the host name and database name that you provide, which is described in the following section.

Figure 2: Completed Edit Database Server Types screen 9. Click Close to create the database server type and close the dialog box.

Creating a Data Source


To add a database to be used as a data source by your application, follow these steps: . 1. In the Servers window, right-click Data Sources and choose Add Data Source from the pop-up menu. 2. In the Add Data Source dialog box, choose a server type from the Server Type drop-down list, then set the values appropriate to your installation in the Data Source Name, Database Name, Host Name, User ID, Password, and Validation Table fields. This tutorial's MySQL example uses the values shown in the following dialog box. Note the Database URL. The parameters #HOSTNAME and #DATABASE that you entered when you created the server type are automatically replaced by the Database Name and Host Name provided in this dialog box.

Figure 3: Completed Add Data Source Dialog Box Note; If you are adding a data source for the Microsoft SQL server driver, and you want Windows authentication, leave the User ID and Password fields blank. The driver automatically uses the Windows user id and password to make a connection to the database. For more information, see The Microsoft SQL Server Driver. 3. Click Test Connection to ensure that the IDE can connect to your database. If you get an error message, verify that the information in the text fields is correct for your database, and ensure that your database server is running. If the error dialog box displays the message Invalid authorization specification, message from server: "Access denied for user: 'user'@'localhost' (Using password:YES)", ensure that 'user' is granted permission on the database server to access the database from localhost. You may need to create a validation table for testing purposes. When your application runs, it obtains database connections from a Sun Java System Application Server connection pool. The application server validates the connection by querying the validation table. The table must exist and be accessible, but it does not require any rows. Do not use an existing table that has a large number of rows or a table that is already frequently accessed. See your database vendor's documentation for more information. 4. Click Add to add the data source.

Summary
The process of creating a data source for a new database consists of two steps: 1. Define a new database server type, in which the URL may reference the parameters #HOSTNAME and #DATABASE. 2. Create a new data source, which instantiates one of the database server types.

See Also: q Using Databound Components to Access Databases q System Requirements lists the supported databases q MySQL Documentation

More Developer Resources: For more tutorials, articles, tips, forums, updates, and expert advice for developers, visit the Java Studio Creator developer resources on the Sun Developer Network (SDN) at http://developers.sun.com/jscreator/.

This page was last modified: September 12, 2006

Sun and Third-party Trademarked Terminology


The following Sun trademarked terms might be used in the Sun Java(tm) Studio Creator tutorials:
q q q q q q q q q

Sun Java Studio Creator integrated development environment (IDE) Sun Java System Application Server version number (Application Server) Java Platform, Standard Edition technology (Java SE(tm) platform) JavaServer(tm) Faces technology JavaServer Pages(tm) technology (JSP(tm) technology) Sun Java System Web Server version number (Web Server) Java Database Connectivity software (JDBC software) Enterprise JavaBeans(tm) specification (EJB(tm) specification) Solaris(tm) Operating System software (Solaris OS software)

The following third-party trademarked terms might be used in the Sun Java Studio Creator tutorials:
q q

UNIX(R) software SPARC(R) processor

Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is described in this document. In particular, and without limitation, these intellectual property rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents or pending patent applications in the U.S. and in other countries. U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. Export Control laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods, or services available on or through any such sites or resources.

Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, tats-Unis. Tous droits rservs. Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans ce document. En particulier, et ce sans limitation, ces droits de proprit intellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autres pays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cup sont des marques de fabrique ou des marques dposes de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations et peut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine des exportations et importations. Les utilisations, ou utilisateurs finaux, pour des armes nuclaires,des missiles, des armes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, y compris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ou indirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la liste de ressortissants spcifiquement dsigns, sont rigoureusement interdites. Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanement mentionns dans ce document et n'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.

Você também pode gostar