这是一个毕业设计项目,我在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&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;
/**
@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数据库中。
请问,这是哪里可能出现了问题?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。