qq_40619811 2022-01-04 17:37 采纳率: 89.8%
浏览 54
已结题

为什么表单上的数据无法插入到mysql数据库中,哪里错了?

你好,我用的是myeclipse。我是初学的javaee。我今天做了一个简单的用户、密码注册程序,用的是mysql数据库,请大家帮我看看我哪里出了问题。我的mysql数据库名为mydb,密码和用户名都是正确的,表名是users,表名和数据库名都是正确的。
JDBC类

package com.deng.jdbc;
import java.sql.*;
public class JDBCConnection {
public static void connect(String username,String password){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","ASD123zxc,./");
        String sql="insert into users(username,password) values (?,?);";
        PreparedStatement pre=con.prepareStatement(sql);
        pre.setString(1, username);
        pre.setString(2,password);
        pre.executeUpdate();
        pre.close();
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}
}



Servlet类

package com.deng.servlet;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.deng.jdbc.JDBCConnection;

public class MyServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    request.setCharacterEncoding("utf-8");
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    JDBCConnection.connect(username, password);
}
}

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>com.deng.servlet.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/theta/index.jsp</url-pattern>
  </servlet-mapping>
</web-app>


跳转后的页面delta.jsp


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'delta.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    This is my delta page. <br>
  </body>
</html>


主页面(也就是index.jsp,有文本框和提交按钮的这个页面)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
  
 <form name="former" method="post" action="/theta/delta.jsp">
 <table border="0">
 <tr>
    <th>用户名</th>
    <th><input type="text" name="username" value="${param.username}"></input></th>
    <tr>
    <th>密码</th>
    <th><input type="password" name="password" value="${param.password}"></input></th>
    </tr>
    <tr>
    <th align="center">
   <input type="submit" name="sure" value="确定"></input>
    </th>
    </tr>
   </table>
    </form>
  </body>
</html>


我不知道是哪里错了,当我点击确定按钮的时候,密码框和用户名里面的文字和密码无法插入到mysql数据库里,点击确定按钮时,mysql数据库没有数据,点击确定按钮后,我的mysql数据库还是没有数据。但是确实能够跳转到delta.jsp的页面,显示This is my delta page. 为什么我的代码只能跳转,却不能插入数据到数据库,这是为什么呢?

  • 写回答

3条回答 默认 最新

  • Kyle_Ping 2022-01-04 17:45
    关注

    表单要提交到servlet才能执行servlet中的代码。也就是,formaction的路径应该是servlet的路径而不是delta的路径(/theta/delta.jsp)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月4日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来