HP 5991-5565 Connecting Tomcat to MySQL or Oracle Using Hibernate, # mysql -u root -p Password

Page 36

Connecting Tomcat to MySQL or Oracle Using Hibernate

Hibernate is an open-source object and relational mapping framework. It uses a lightweight object encapsulation for JDBC and allows JAVA developers to access data from databases using JAVA objects. Hibernate is designed to run in a managed environment, such as the environment of the web server. With Hibernate, you only need to configure a small part of the environment. Hibernate inherits database connections from the application server. Therefore, it is easy to create an enterprise application that uses Hibernate capabilities in the same way as other capabilities available in the application server. You can obtain Hibernate and Hibernate tools from the product websites. See Table 1 (page 12) for the appropriate links.

This section provides an example demonstrating the use of Hibernate for web applications using a MySQL or Oracle database.

Installing Hibernate, Tomcat, and JDK

Download the Hibernate package, including tools, from the Hibernate website and install it using the instructions provided. See Table 1 (page 12) for the correct version and website link.

Verify that you have installed and configured both Sun JDK and Tomcat before proceeding. For additional information, see “Installing and Configuring Tomcat” (page 17).

Configuring a Database Driver in Tomcat

Verify that MySQL or Oracle are configured as data sources for Tomcat as described in “Connecting Tomcat to a Database” (page 30) before proceeding.

To verify that the mysql-connector-java-<VERSION>.jar or ojdbc14.jar are configured, go to the $CATALINA_HOME/common/lib directory and verify that the corresponding file exists.

Adding Test Data to the Databases

In order to verify that the MySQL or Oracle database interact properly with Tomcat, you must populate the database with test data.

Add test data to the MySQL database using the following steps:

1.Log in to the MySQL command line tool by entering the following command:

# mysql -u root -p <PASSWORD>

2.Create a database named osmsdb and create a table named USERS by entering the following commands:

mysql> create database osmsdb; mysql> use osmsdb;

mysql> create table USERS(

mysql> User_id int(10) auto_increment not null primary key, mysql> First_name varchar(30) not null,

mysql> Last_name varchar(30) not null mysql> );

3.Add a user named osmsusr that uses the password osmspass and grant the appropriate privileges to it by entering the following commands

mysql> create user 'osmsusr'@'%.%.%.%';

mysql> grant all on osmsdb.* to 'osmsusr'@'%.%.%.%' identified \ by 'osmspass';

Add test data to the Oracle database using the following steps:

36

Image 36
Contents HP Open Source Middleware Stacks Blueprint Copyright 2007 Hewlett-Packard Development Company, L.P Table of Contents Monitor Tomcat with OpenView Tomcat SPI Webalizer Usage Report Oracle Database TestPage List of Tables Page HP Services IntroductionExecutive Summary Intended AudienceTypographic Conventions HP Encourages Your CommentsUserInput Feedback@fc.hp.comHP Open Source Web Server Middleware Stack Web Server Stack ArchitectureWeb Server Stack Architecture Installing and Verifying the Linux Distribution Installing the Linux DistributionRequired Packages and Installation Recommendations Verifying the Linux Distribution Installation YaST Software Selection and System TasksConfiguring the Basic Apache Http Server Installing and Configuring Sun JDK on an HP Proliant SystemTomcat # /etc/init.d/apache2 start# ln -s /usr/java/jdk1.5.010 /usr/local/jdk For the alljava.sh file, add the following linesFor the alljava.csh file, add the following lines Installing and Configuring Tomcat # java -versionFollowing messages appear # /usr/local/apache-tomcat/bin/startup.shAdvanced Apache Http Server Features Using the Apache Http Server Benchmarking ToolIf necessary, stop Tomcat by entering the following command # /usr/local/apache-tomcat/bin/shutdown.shYou can correct the error by entering the following Apache Http Server Status and InformationApachemodules definition should now look like the following Restart the Apache Http Server for the changes take effectApache Server Status web page opens, as shown in Figure Apache Server Information web page opens, as shown in FigureApache Http Server Log # /etc/init.d/apache2 restart# webalizer Using Virtual Host Support For detailed information, see the Webalizer website atCreate the following file Using Apache Http Server URL Redirection Configure virtual hosting by creating a file namedOn the second server, create the following Html file Using Apache Http Server Proxy Features Edit the following configuration fileAdd the following line to the preceding file Add the following lines to the preceding fileError Responses and Redirects Create the following Html fileUsing Per-User Web Directories Populate the preceding file with the following linesConfiguring the Apache and Tomcat Connector Integrating the Web Server Stack ComponentsRestart Apache by entering the following command If there are any errors, check the following log filesInsert the following line below the DocumentRoot line Http//YOURHOSTNAME/jsp-examplesIntegrating the Apache Web Server and PHP Integrating the Apache Web Server and Perl# rpm -q php5 apache2-modphp5 Create the following test fileConnecting Tomcat to a Database Integrating the Apache Web Server and PythonWeb page displays the following text Connecting Tomcat and MySQL # tar zxf mysql-connector-java-version.tar.gz# mysqladmin -u root -p create osmsdb # $CATALINAHOME/bin/startup.sh# ps -efgrep mysqld # /etc/init.d/mysql startConnecting Tomcat and Oracle If needed, start Tomcat by entering the following commandHttp//YOURHOSTNAME8080/mysqltest.jsp Test page opens as shown in FigureObtaining and Installing the Oracle Jdbc Driver Configuring the Oracle ServerTesting the Oracle Database Connectivity Creating an Oracle Data SourceHttp//YOURHOSTNAME8080/oracleTest.jsp Oracle Database TestConnecting Tomcat to MySQL or Oracle Using Hibernate Installing Hibernate, Tomcat, and JDKConfiguring a Database Driver in Tomcat # mysql -u root -p PasswordCreating a Simple Application in Tomcat # sqlplus osmsusr/osmspass@osmsdboraFor a MySQL database server For an Oracle database serverAsm.jar asm-attrs.jar antlr-2.7.6rc1.jar File HibernateSessionFactory.class is generated Preceding command creates a class file named This directory, create a Hibernate Pojo file namedVerifying the Simple Application Operation Http//YOURHOSTNAME8080/SimpleDemo/hibernate.jspSecure the Web Server Stack Enabling Https Support in the Apache Http ServerRestart Apache by entering the following Apache Http Server Authorization Apache default document root isVerify that the certificate is displayed as in Figure Create the following test Html fileProtecting Apache Http Server Authorization with Symas CDS If the login fails, the following message appearsConfiguring Apache Http Server Services Protecting Tomcat Authorization with Symas CDS Html HeadHtml Head Monitor Tomcat with OpenView Tomcat SPI # $CATALINA/bin/shutdown.sh # $CATALINA/bin/startup.shSoftware Prerequisites and Reference Guides Tomcat SPI ComponentsTools PoliciesComplete these steps Complete these steps Appendix B Frequently Asked Questions # /sbin/SuSEfirewall2 stopAppendix C Vendor Information Reference Following are the contents of the hibernate.jsp file Appendix D Hibernate Test Application Source CodePage Appendix D Hibernate Test Application Source Code