Source: http://stephen830.iteye.com/blog/2001939
Install MySQL
MySQL Community Server (GPL) 5.6.15
Installing tomcat7.0
http://tomcat.apache.org/download-70.cgi
The download is in the tar version
Installing jdk1.7
Install Eclipse
When you start eclipse and say you can't find the JDK, you automatically download a 1.6 jdk.
After Eclipse was launched, it found that there were 2 JRE.
Or select the 1.7 jdk that was previously installed.
Configuring environment Variables Jdk_home and Catalina_home
Open a terminal:
Ruby Code
- CD ~
Switch to your user directory, create a new file, filename. bash_profile
Input content
Export Java_home= your JDK directory
Export Catalina_home= your Tomcat directory
JDK directory you can copy directly from the configuration information of Eclipse
Start Tomcat
CD $CATALINA _home/bin
./startup.sh
Open Browser input, http://localhost:8080/
If there is a display, it indicates a successful configuration.
Download the Java driver for MySQL and place it in the Tomcat Lib directory.
Create a new Dynamic web App on Eclipse, configure the Jndi database connection pool,
Add a file under Meta-inf context.xml
XML code
- <Context>
- <!--MySQL DBCP --
- <Resource name="Jdbc/mydb" auth="Container" type="Javax.sql.DataSource"
- driverclassname= "com.mysql.jdbc.Driver" url="Jdbc:mysql://localhost:3306/mydb "
- username= "mydb" password="123456" maxactive= "" "maxidle="
- maxwait="10000" />
- </Context>
Add the following in Web. XML under Web-inf:
XML code
- <resource-ref>
- <description>my DB Connection</description>
- <res-ref-name>jdbc/mydb</res-ref-name>
- <res-type>javax.sql.datasource</res-type>
- <res-auth>container</res-auth>
- </resource-ref>
Before you run the test, make sure that you have the databases and users in your MySQL that are already in use in your configuration.
To test the database code:
Java code
- <%@ page language="java" contenttype="text/html; Charset=utf-8 "
- pageencoding="UTF-8"%>
- <%@ page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*"%>
- <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c8>
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <body>
- <%
- Context CTX = new InitialContext ();
- String strlookup = "Java:comp/env/jdbc/mydb";
- DataSource ds = (DataSource) ctx.lookup (strlookup);
- Connection con = ds.getconnection ();
- if (con! = null) {
- Out.print ("Success");
- }else{
- Out.print ("failure");
- }
- %>
- </body>
Configuration of Eclipse+tomcat+mysql on Mac