how to connect tomcat 7 and mysql

2019-10-03 11:00发布

i m quite new to j2ee..... i just finished reading head first servlets and jsp ....

for a practice project i was thinking to build a money tracker in which i can save my expenses and see them anytime in the future.... but for this i need a database , i googled for it and found that mysql is a good database but i know nothing about how to set tomcat to talk to mysql....

and i m using eclipse kepler with tomcat 7 .......

plz tell me what are the steps to follow to configure tomcat so that it can see mysql and i can access the database through my codes ......

i m on windows 8.

it seems like setting up things is more difficult than coding plz plz plz help me

1条回答
家丑人穷心不美
2楼-- · 2019-10-03 11:40

These are some steps you could do:

  1. Create a context.xml and save it under TOMCAT_HOME/conf.

  2. Into the context.xml you have to add your DB configuration which could look like:

    <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" maxActive="510" minIdle="0" maxIdle="200" maxWait="10000" username="test" password="test" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/test" poolPreparedStatements="true" maxOpenPreparedStatements="510" validationQuery="SELECT 1" testOnBorrow="true" />

  3. In the web.xml of your application refer to the above context, like:

    <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

  4. Put the JAR (e.g. mysql-connector-java-5.1.12-bin.jar) containing the mysql driver into TOMCAT_HOME/lib

I hope it will help.

查看更多
登录 后发表回答