问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
JSP页面
`<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="rg_layout">
<!-- 页面左部分-->
<div class="rg_left">
<h1>新用户注册</h1>
<h2>USER REGISTER</h2>
</div>
<!--页面中间部分-->
<div class="rg_center">
<div class="rg_form ">
<!--定义表单 form-->
<form action="RegisterBigProject" id="form" method="post">
<table>
<!-- 用户名-->
<tr>
<td class="td_left"><label for="username">用户名</label></td>
<td class="td_right">
<input type="text" name="username" id="username" placeholder="请输入用户名">
<span id="s_username" class="error"></span>
</td>
</tr>
<!-- 密码-->
<tr>
<td class="td_left"><label for="password">密码</label></td>
<td class="td_right">
<input type="password" name="password" id="password" placeholder="请输入密码">
<span id="s_password" class="error"></span>
</td>
</tr>
<!--email-->
<tr>
<td class="td_left"><label for="email">Email</label></td>
<td class="td_right">
<input type="email" name="email" id="email" placeholder="请输入邮箱">
<span id="s_email" class="error"></span>
</td>
</tr>
<!--姓名-->
<tr>
<td class="td_left"><label for="name">姓名</label></td>
<td class="td_right"><input type="text" name="name" id="name" placeholder="请输入姓名"></td>
</tr>
<!--手机号-->
<tr>
<td class="td_left"><label for="tel">手机号</label></td>
<td class="td_right">
<input type="text" name="tel" id="tel" placeholder="请输入手机号">
<span id="s_tel" class="error"></span>
</td>
</tr>
<!--性别-->
<form action="RegisterBigProject" method="post">
<tr>
<td class="td_left" ><label >性别</label></td>
<td class="td_right">
<input type="radio" name="gender" value="male" checked > 男
<input type="radio" name="gender" value="female"> 女
<!-- - <input type="text" name="tel" id="tel" placeholder="请输入性别">-->
</td>
</tr>
</form>
<!--出生日期-->
<tr>
<td class="td_left"><label for="birthday">出生日期</label></td>
<td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="请输入出生日期">
</td>
</tr>
</form>
<!--验证码
<tr>
<td class="td_left"><label for="checkcode">验证码</label></td>
<td class="td_right">
<input type="text" name="checkcode" id="checkcode" placeholder="请输入验证码">
<span id="s_checkcode" class="error"></span>
</td>
</tr>-->
<!--注册按钮-->
<tr>
<td colspan="2" id="td_sub">
<input type="submit" id="btn_sub" value="注册">
</td>
</tr>
</table>
</form>
</div>
</div>
<!--页面右部分-->
<div class="rg_right">
<p>已有账号?<a href="Login.jsp">立即登录</a></p>
</div>
</div>
</body>
</html>
``
Servlet文件
```java
package Test;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
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 com.mysql.jdbc.PreparedStatement;
/**
* Servlet implementation class RegisterBigProject
*/
@WebServlet("/RegisterBigProject")
public class RegisterBigProject extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterBigProject() {
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());
String dbName = "test2";
String dbUserName = "root";
String dbPassWord="";
String url = "jdbc:mysql://localhost:3306/"+dbName+"?user"+dbUserName+"&password="
+dbPassWord+"&useUnicode=true&characterEncoding=UTF-8";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,dbUserName,dbPassWord);
String sql="insert into user(username,password,email,name,phone,sex,birthday)values(?,?,?,?,?,?,?)";
PreparedStatement stmp=(PreparedStatement) conn.prepareStatement(sql);
request.setCharacterEncoding("UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String email=request.getParameter("email");
String name=request.getParameter("name");
String phone=request.getParameter("phone");
//Integer i = Integer.parseInt("phone");
//long l = new long(phone);
String sex=request.getParameter("sex");
String birthday=request.getParameter("birthday");
stmp.setString(1,username);
stmp.setString(2,password);
stmp.setString(3,email);
stmp.setString(4,name);
stmp.setString(5,phone);
stmp.setString(6,sex);
stmp.setString(7,birthday);
int n=stmp.executeUpdate();
// if(n.next() && n!=null){
// if (n.getString("Name").equals(Name)) {
// String a = "注册成功";
// request.getRequestDispatcher("/sucess.jsp").forward(request, response);
// }
// } else{
// String b = "注册失败";
// }
request.getRequestDispatcher("/Login.jsp").forward(request, response);
stmp.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @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);
}
}
#运行结果及报错内容
phone(bigint)类型和sex(enum)从Web工程(String)传输到Mysql时为空值
我的解答思路和尝试过的方法
尝试转换MySQL数据类型,无效
我想要达到的结果
phone和sex字段可以由JDBC技术正常插入数据