您好,欢迎访问一九零五行业门户网

MySQLDBCPExample_MySQL

0. introductionversions of mysql and jdbc drivers that have been reported to work:
mysql 3.23.47, mysql 3.23.47 using innodb,, mysql 3.23.58, mysql 4.0.1alphaconnector/j 3.0.11-stable (the official jdbc driver)mm.mysql 2.0.14 (an old 3rd party jdbc driver)before you proceed, don't forget to copy the jdbc driver's jar into $catalina_home/lib.
1. mysql configurationensure that you follow these instructions as variations can cause problems.
create a new test user, a new database and a single test table. your mysql user must have a password assigned. the driver will fail if you try to connect with an empty password.
mysql> grant all privileges on *.* to javauser@localhost -> identified by 'javadude' with grant option;mysql> create database javatest;mysql> use javatest;mysql> create table testdata ( -> id int not null auto_increment primary key, -> foo varchar(25), -> bar int);
note: the above user should be removed once testing is complete!next insert some test data into the testdata table.
mysql> insert into testdata values(null, 'hello', 12345);query ok, 1 row affected (0.00 sec)mysql> select * from testdata;+----+-------+-------+| id | foo | bar |+----+-------+-------+| 1 | hello | 12345 |+----+-------+-------+1 row in set (0.00 sec)mysql>
2. context configurationconfigure the jndi datasource in tomcat by adding a declaration for your resource to your context.
for example:

3. web.xml configurationnow create a web-inf/web.xml for this test application.
mysql test app db connection jdbc/testdb javax.sql.datasource container
4. test codenow create a simple test.jsp page for use later.
select id, foo, bar from testdata db test results foo ${row.foo}
bar ${row.bar}

that jsp page makes use of jstl's sql and core taglibs. you can get it from apache tomcat taglibs - standard tag library project — just make sure you get a 1.1.x release. once you have jstl, copy jstl.jar andstandard.jar to your web app's web-inf/lib directory.
finally deploy your web app into $catalina_base/webapps either as a warfile called dbtest.war or into a sub-directory called dbtest
once deployed, point a browser at http://localhost:8080/dbtest/test.jsp to view the fruits of your hard work.
其它类似信息

推荐信息