Mac上eclipse+tomcat+mysql的配置

來源:互聯網
上載者:User

標籤:

來源: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代碼  
  1. 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代碼  
  1.       
  2.     <Context>  
  3.     <!-- MySQL DBCP -->  
  4.     <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"  
  5.         driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mydb"  
  6.         username="mydb" password="123456" maxActive="100" maxIdle="30"  
  7.         maxWait="10000" />  
  8. </Context>  

 

 

 

在WEB-INF下的web.xml中添加如下內容:

 

Xml代碼  
  1. <resource-ref>  
  2.       <description>my DB Connection</description>  
  3.       <res-ref-name>jdbc/mydb</res-ref-name>  
  4.       <res-type>javax.sql.DataSource</res-type>  
  5.       <res-auth>Container</res-auth>  
  6.     </resource-ref>  

 

 

 

 

在測試回合之前,請確保你的mysql中已經有配置中使用到的資料庫和使用者了。

 

 

測試資料庫代碼:

 

Java代碼  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. </head>  
  9. <body>  
  10. <%  
  11. Context ctx = new InitialContext();          
  12. String strLookup = "java:comp/env/jdbc/mydb";     
  13. DataSource ds =(DataSource) ctx.lookup(strLookup);    
  14. Connection con = ds.getConnection();    
  15. if (con != null){    
  16.     out.print("success");    
  17. }else{    
  18.     out.print("failure");    
  19. }           
  20. %>  
  21. </body>  
  22. </html>  

 

 

 

 

 

 

 

 

  • 查看圖片附件

Mac上eclipse+tomcat+mysql的配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.