• linux下tomcat连接mysql数据库


    一.下载安装jdk,并配置Java环境
    官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
    以jdk-7u79-linux-i586.tar.gz为例:
    将jdk-7u79-linux-i586.tar.gz解压并放在/usr/local/下:
    解压方法(tar -zxvf jdk-7u79-linux-i586.tar.gz)以下类似

    配置环境变量:
    在 /etc/profile添加一下语句:

    export JAVA_HOME=/usr/local/jdk1.7.0_79
    export CLASS_PATH=/usr/local/jdk1.7.0_79/lib
    export PATH=$JAVA_HOME/bin:$PATH

    保存退出 运行命令 source /etc/profile 使配置立即生效

    看Java配置是否正确通过Java命令,如果系统识别该命令说明配置成功!

    二.下载安装tomcat
    官网下载地址:http://tomcat.apache.org/
    以为例:apache-tomcat-8.5.0.tar.gz为例:
    将apache-tomcat-8.5.0.tar.gz解压并重命名为tomcat并放在/usr/local/下:

    配置环境变量:
    在 /etc/profile添加一下语句:

    export TOMCAT_HOME=/usr/local/tomcat
    保存退出 运行命令 source /etc/profile 使配置立即生效

    启动tomcat服务器
    cd /usr/local/tomcat/bin
    ./startup.sh
    出现下面的语句则启动tomcat成功:
    Using CATALINA_BASE: /usr/local/tomcat
    Using CATALINA_HOME: /usr/local/tomcat
    Using CATALINA_TMPDIR: /usr/local/tomcat/temp
    Using JRE_HOME: /usr/local/jdk1.7.0_79
    Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    Tomcat started.

    在浏览器中输入
    http://localhost:8080/(如果不是本机,则输入对应的ip地址)
    测试出现tomcat页面则测试成功

    3.下载 mysql-connector-java
    官网下载地址:http://dev.mysql.com/downloads/connector/j/
    以为例:mysql-connector-java-5.1.25.tar.gz为例:
    将mysql-connector-java-5.1.25.tar.gz解压并放在/usr/local/下:
    cd mysql-connector-java-5.1.25

    cp mysql-connector-java-5.1.25-bin.jar /usr/local/tomcat/lib

    重启tomcat
    cd /usr/local/tomcat/bin
    ./shutdown.sh
    ./startup.sh
    4.测试tomcat是否能连接Mysql
    cd /usr/local/tomcat/webapps
    mkdir test
    touch index.jsp
    vim index.jsp
    编辑如下内容:

    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%@ page import="java.naming.*" %>
    <%@ page import="java.sql.*" %>
    <html>
    <head>
    <title>My first DB</title>
    </head>

      <body>

    <h1>
    <%
    try {
    //加载驱动
    Class.forName("com.mysql.jdbc.Driver");
    //建立连接 注:下面的test为数据库名字,root为mysql用户名 123456 为root的密码
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
    //创建状态
    Statement state=con.createStatement();
    //插入
    //String sql="insert into student values('id','age'),('2','20')";
    //state.executeUpdate(sql);
    //更新
    //String sql1="update student set id='001' where name='liu'";
    // state.executeUpdate(sql1);
    //修改
    //String sql3="update student set name='liu' where id='2'";
    //state.executeUpdate(sql3);
    //查询
    //String sql2="select id,name from student where id='1'";

    String sql2="select * from student";
    ResultSet rs=state.executeQuery(sql2);
    while(rs.next()){
    String id=rs.getString("id");
    String name=rs.getString("name");

    out.println("id:"+id+" "+"name:"+name+"<br>");
    }
    out.println("success..."+"<br>");
    state.close();
    con.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    %>

    </h1>

    </body>

    </html>

    在浏览器中输入
    http://localhost:8080/test/index.jsp
    出现类似
    id:2 name:Tom
    id:3 name:Huhu
    success...
    表示成功!

    注若是没有安装数据库可参考另一篇文章 http://www.cnblogs.com/liuzhennan/articles/5319280.html

  • 相关阅读:
    姐姐的vue(1)
    LeetCode 64. Minimum Path Sum 20170515
    LeetCode 56. 56. Merge Intervals 20170508
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 24. Swap Nodes in Pairs 20170424
    LeetCode 19. Remove Nth Node From End of List 20170417
    LeetCode No.9 Palindrome Number 20170410
    LeetCode No.8. String to Integer (atoi) 2017/4/10(补上一周)
    LeetCode No.7 Reverse Integer 2017/3/27
    LeetCode No.4 Median of Two Sorted Arrays 20170319
  • 原文地址:https://www.cnblogs.com/liuzhennan/p/5371880.html
Copyright © 2020-2023  润新知