Tomcat+MySql+jdbc

  1. 云栖社区>
  2. 博客>
  3. 正文

Tomcat+MySql+jdbc

科技探索者 2017-11-22 23:09:00 浏览681
展开阅读全文

 

Tomcat+MySql+jdbc

 

Linux下安装配置 Jdk+Tomcat+MySql+jdbc
 
1、安装JDK
下载 jdk-7-linux-i586.rpm 
http://java.sun.com/javase/downloads/index.jsp 
# rpm –ivh jdk-7-linux-i586.rpm   
  
# vi ~/.bash_profile  
JAVA_HOME=/usr/java/jdk1.7.0  
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib  
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin  
export PATH CLASSPATH JAVA_HOME  
使环境变量立刻生效
# source ~/.bash_profile
# source /etc/profile
# java -version (jdk的版本为jdk1.4.2则表示jdk已成功安装)。  
2、安装Tomcat  
下载apache-tomcat-6.0.10.tar.gz  
http://jakarta.apache.org/tomcat
# tar zxf apache-tomcat-6.0.10.tar.gz -C /usr/src
# mv /usr/src/apache-tomcat-6.0.10   /usr/src/tomcat  
# /usr/src/tomcat/bin/startup.sh           (启动tomcat)  
                或
# /usr/src/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat  
Using CATALINA_HOME: /usr/local/tomcat  
Using CATALINA_TEMDIR: /usr/local/tomcat/temp  
Using JAVA_HOME: /usr/java/jdk1.7.0 
# /usr/src/tomcat/bin/catalina.sh start/stop  (启动/关闭tomcat)
到此tomcat已经安装完成,现在使用浏览器访问 http://localhost:8080,出现tomcat默认页面,

系统重启后自动启动Tomcat:
[root@tpwb ~]# vi /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
export JDK_HOME=/usr/java/jdk1.7.0
export JAVA_HOME=/usr/java/jdk1.7.0
/usr/src/tomcat/bin/catalina.sh start
 
设置虚拟主机:

# vi /usr/src/tomcat/conf/server.xml
<Host name="www.benet.com" debug="0" appBase="/usr/src/tomcat/webapps/ROOT" unpackWARs="true"

autoDeploy="true" xmlValidation="false"  xmlNamespaceAware="false">  
     <Context path="" docBase="." debug="0" reloadable="true" crossContext="true"/>  
     <Valve className="org.apache.catalina.vales.AccessLogValve" directory="logs"

prefix="www.benet.com_access_log." pattern="common" suffix=".txt" 
timestamp="true"/> 
      </Host>

<Host name="www.benet.com" debug="0" appBase="/usr/src/tomcat/webapps" unpackWARs="true"

autoDeploy="true" xmlValidation="false"  xmlNamespaceAware="false">  
     <Context path="" docBase="ROOT" debug="0" reloadable="true" crossContext="true"/>  
     <Valve className="org.apache.catalina.vales.AccessLogValve" directory="logs"

prefix="www.benet.com_access_log." pattern="common" suffix=".txt" 
timestamp="true"/> 
      </Host>
 
一个jsp页面:
# mkdir /usr/src/tomcat/webapps/ROOT/test
# cd /usr/src/tomcat/webapps/ROOT/test
# vi hello.jsp
<% out.println("Hello World!"); %>
:wq
 
访问:http://localhost:8080/test/hello.jsp
 
附:
    
       tomcat默认是不开启目录浏览的,可以通过修改web.xml里的listings的值来设定,改false为true。如

下:
        <param-name>listings</param-name>
            <param-value>true</param-value>
      默认不开启servlet直接访问,可以修改web.xml和context.xml的配置来开启,如下:
        web.xml。找到这段,去掉注释 (<-- -->)
        <servlet-name>invoker</servlet-name>
            <servlet-class>
              org.apache.catalina.servlets.InvokerServlet
            </servlet-class>
        还有这段,也去掉注释
        <servlet-mapping>
            <servlet-name>invoker</servlet-name>
        
        context.xml
在Context里增加privileged
        <Context privileged="true">

3、安装mysql

http://down1.chinaunix.net/distfiles/mysql-5.0.56.tar.gz

# useradd -M -s /sbin/nologin  mysql
# tar zxf mysql-5.0.56.tar.gz -C /usr/src
# cd /usr/src/mysql-5.0.56
# ./configure --prefix=/usr/local/mysql --with-unix-sock-path=/var/lib/mysql/mysql.sock
# make
# make install
# cp support-files/my-medium.cnf /etc/my.cnf
# vi /etc/my.cnf
socket=/var/lib/mysql/mysql.sock
:wq
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.mysql /usr/local/mysql/
# chown -R mysql /usr/local/mysql/var
# echo "/usr/local/mysql/lib/mysql"  >> /etc/ld.so.conf
# ldconfig
# /usr/local/mysql/bin/mysqld_safe  --user=mysql &
# 回车(Enter键)
# netstat -ntpl | grep 3306
# cp support-files/mysql.server  /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --level 35 mysqld on
# export PATH=$PATH:/usr/local/mysql/bin
# echo "PATH=$PATH:/usr/local/mysql/bin"   >>  /etc/profile
# mysqladmin -u root password "123456"
# mysql -u root -p
4. mysql-connector-java安装
http://dev.mysql.com/downloads/connector/j/5.1.html
    4.1 解压缩,把jar文件移到tomcat lib目录下    
# tar -zxvf mysql-connector-java-5.1.6.tar.gz 
# cp mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar /usr/src/local/tomcat/lib/
    4.2   配置数据池连接
    4.2 .1  修改tomcat/conf/server.xml,对原来test项目进行修改,添加数据池配置
    <Host name="www.benet.com" debug="0" appBase="/usr/src/tomcat/webapps/ROOT" unpackWARs="true"

autoDeploy="true" xmlValidation="false"  xmlNamespaceAware="false">  
  <Resource  name="jdbc/test"
             auth="Container"
    type="javax.sql.DataSource"
             maxActive="100" maxIdle="30" maxWait="10000"
             username="root" password=""
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/test_db_name?

autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
 </Context> 
    4.2 .2  在/usr/src/tomcat/webapps/ROOT/WEB-INF/web.xml文件,加入如下内容:
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>My Test App</description>
     <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>
  </web-app>

   4.2.3  mysql.jsp 测试 (先在mysql数据库里建个数据库phpbb,表phpbb,用户phpbb,密码phpbb)
# mysql -u root -p
> create database phpbb;
> use phpbb;
> create table phpbb(user_name char(30) not null,user_passwd char(20) not null);
> insert into phpbb.phpbb(user_name,user_passwd) values('phpbb',encrypt('123456'));
> grant all on phpbb.* to phpbb@'localhost' identified by 'phpbb';
> quit
# vi mysql.jsp
<%@ page contentType="text;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/phpbb?

user=phpbb&password=phpbb&useUnicode=true&characterEncoding=8859_1";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="show tables";
ResultSet rs=stmt.executeQuery(sql);%>
<%while(rs.next()) {%>
<%=rs.getString(1)%>
<br>
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

 访问:http://localhost:8080/test/mysql.jsp

本文转自linux博客51CTO博客,原文链接http://blog.51cto.com/yangzhiming/835046如需转载请自行联系原作者


yangzhimingg

网友评论

登录后评论
0/500
评论
科技探索者
+ 关注