標籤:
來源:http://stephen830.iteye.com/blog/2001939
安裝 mysql
MySQL Community Server (GPL) 5.6.15
安裝 tomcat7.0
http://tomcat.apache.org/download-70.cgi
下載的是tar版
安裝 jdk1.7
安裝 eclipse
在啟動eclipse的時候說找不到jdk,就自動下載了一個1.6的jdk。
eclipse啟動後發現裡面有2個jre。
還是選擇先前安裝的1.7的jdk。
配置環境變數 JDK_HOME和CATALINA_HOME
開啟一個終端:
Ruby代碼
- cd ~
切換到你的使用者目錄下,建立一個檔案,檔案名稱 .bash_profile
輸入內容
export JAVA_HOME=你的jdk目錄
export CATALINA_HOME=你的tomcat目錄
jdk目錄你可以直接從eclipse的配置資訊中拷貝
啟動tomcat
cd $CATALINA_HOME/bin
./startup.sh
開啟瀏覽器 輸入,http://localhost:8080/
如果有顯示,表示配置成功。
下載mysql的java驅動程式,放在tomcat的lib目錄下。
在eclipse上建立新的動態web應用,配置jndi資料庫連接池,
在META-INF下新增檔案 context.xml
Xml代碼
-
- <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="100" maxIdle="30"
- maxWait="10000" />
- </Context>
在WEB-INF下的web.xml中添加如下內容:
Xml代碼
- <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>
在測試回合之前,請確保你的mysql中已經有配置中使用到的資料庫和使用者了。
測試資料庫代碼:
Java代碼
- <%@ 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">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- </head>
- <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>
- </html>
Mac上eclipse+tomcat+mysql的配置