qq_40619811
2022-01-04 17:37
采纳率: 75%
浏览 27

为什么表单上的数据无法插入到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条回答 默认 最新

相关推荐 更多相似问题