一、安裝java
html
sudo apt-get -y install default-jdk
二、安裝tomcat7
java
apt-get install -y tomcat7 tomcat7-admin tomcat7-docs tomcat7-examples
tomcat幾個經常使用目錄介紹: mysql
root @yang :/var/lib/tomcat7/webapps/ROOT# pwd<% out.println("<h1>hello world</h1>"); out.println( new java.util.Date() ); %>
三、配置tomcat web
root@yang :/etc/tomcat7# cd
root@yang :~# cd /etc/tomcat7/
root@yang :/etc/tomcat7# cat tomcat-users.xml #註冊一個tomcat管理員用戶
sql
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> </tomcat-users>
四、重啓tomcat shell
service tomcat7 restart
六、mysql數據庫安裝
數據庫
sudo apt-get -y install mysql-server mysql-common mysql-client mysqladmin -u root password 'root' echo "create database db ; create table tb ( name varchar(20),age int ) ; insert into tb values('name',26) ;" | mysql -uroot -proot
五、一個鏈接mysql的例子
瀏覽器
(1)、下載java鏈接mysql的驅動tomcat
http://download.csdn.net/detail/u014436218/7217805
app
root@yang:~/Downloads# ll mysql-connector-java-5.1.30.zip -rw-r--r-- 1 yang yang 4058136 6月 8 22:34 mysql-connector-java-5.1.30.zip root@yang:~/Downloads# unzip -x mysql-connector-java-5.1.30 Archive: mysql-connector-java-5.1.30.zip
(2)、安裝驅動
root@yang:~/Downloads/mysql-connector-java-5.1.30# cp mysql-connector-java-5.1.30-bin.jar /usr/share/java/ root@yang:~/Downloads/mysql-connector-java-5.1.30# ll /usr/share/java/mysql-connector-java-5.1.30-bin.jar -rw-r--r-- 1 root root 954041 6月 8 23:47 /usr/share/java/mysql-connector-java-5.1.30-bin.jar root@yang:~/Downloads/mysql-connector-java-5.1.30# ln -s /usr/share/java/mysql-connector-java-5.1.30-bin.jar \ /usr/share/tomcat7/lib/mysql-connector-java-5.1.30-bin.jar
(3)、測試鏈接是否成功
root@yang:/var/lib/tomcat7/webapps/ROOT# cat mysql.jsp <%@ page language="java" import="java.sql.*"%> <html> <head><title>Read from mySQL Database</title> </head> <body> <p align="center"><b>Following records are selected from the 'jakartaproject' table.</b><br> </p> <div align="center" width="85%"> <center> <table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="658" height="63"> <tbody> <td bgColor="#008080" width="47" align="center" height="19"><font color="#ffffff"><b>Sr. No.</b></font></td> <td bgColor="#008080" width="107" height="19"><font color="#ffffff"><b>Project</b></font></td> <td bgColor="#008080" width="224" height="19"><font color="#ffffff"><b>Url Address</b></font></td> <td bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Description of the project</b></font></td> <% String DRIVER = "org.gjt.mm.mysql.Driver"; Class.forName(DRIVER).newInstance(); Connection con=null; ResultSet rst=null; Statement stmt=null; try{ String url="jdbc:mysql://localhost/db?user=root&password=root"; int i=1; con=DriverManager.getConnection(url); stmt=con.createStatement(); rst=stmt.executeQuery("select * from tb "); while(rst.next()){ if (i==(i/2)*2){ %> <tr> <td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><%=i%>.</td> <td bgColor="#ffff98" vAlign="top" width="107" height="19"><%=rst.getString(1)%></td> <td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(2)%></td> </tr> <% }else{ %> <tr> <td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><%=i%>.</td> <td bgColor="#ffcc68" vAlign="top" width="107" height="19"><%=rst.getString(1)%></td> <td bgColor="#ffcc68" vAlign="top" width="270" height="19"><%=rst.getString(2)%></td> </tr> <% } i++; } rst.close(); stmt.close(); con.close(); }catch(Exception e){ System.out.println(e.getMessage()); } %> </tbody> </table> </center> </div> </body> </html>