开发者社区> 问答> 正文

阿里云ECS学生服务器上tomcat与MySQL的连接

这是一个毕业设计项目,我在tomcat中使用servlet去读写同一台ecs服务器上的数据库MySQL 。我使用了tomcat内置的DBCP连接池,在项目的META-INF下配置context.xml配置文件,然后在WEB-INF下的web.xml中进行了配置。
context.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>

<Resource name="jdbc/huoyundataSource" 
    auth="Container" 
    type="javax.sql.DataSource" 
    username="root" 
    password="856368" 
    maxIdle="30" 
    maxWaitMillis="1000" 
    maxTotal="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/huoyundatabase?useServerPrepStmts=true&amp;cachePrepStmts=true"
 />

web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

                  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1"
metadata-complete="true">

Tomcat Test


<description>DB Connection</description>
<res-ref-name>jdbc/huoyundataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

然后读写数据库的servlet代码如下:

package myservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**

  • Servlet implementation class orderInServletTest
    */

@WebServlet("/orderInServletTest")
public class orderInServletTest extends HttpServlet {

private static final long serialVersionUID = 1L;
   
/**
 * @see HttpServlet#HttpServlet()
 */
public orderInServletTest() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //response.getWriter().append("Served at: ").append(request.getContextPath());
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();

    try {
        Context ctx=new InitialContext();
        DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/huoyundataSource");
        Connection con=ds.getConnection();
        String sql="insert into ordertest(USERID,USERPHONE,FIRSTADDR)values(?,?,?)";

        PreparedStatement sta = (PreparedStatement)con.prepareStatement(sql);
        sta.setString(1, "chen");
        sta.setString(2, "15980590666");
        sta.setString(3, "zhenzhou");
        sta.executeUpdate(sql);
        sta.close();
        con.close();

    } catch (Exception e) {

    }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

tomcat使用的版本为9.0,MySQL用的是8.0的版本,servlet可以通过外网正常的访问到,但就是无法将数据插入到MySQL数据库中。

请问,这是哪里可能出现了问题?

展开
收起
bug_developer 2019-05-18 12:03:35 4668 0
1 条回答
写回答
取消 提交回答
  • image
    这里把异常打印出来 看一下报什么错

    2019-07-17 23:35:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
ECS技术实战指南 立即下载
ECS成熟度评估&洞察 立即下载
冬季实战营第一期:从零到一上手玩转云服务器 立即下载

相关镜像