02.225 2021-11-24 15:31 采纳率: 37.5%
浏览 81
已结题

MySQL数据类型与Java类型转换

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

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);
    }

}


#运行结果及报错内容

img


phone(bigint)类型和sex(enum)从Web工程(String)传输到Mysql时为空值

我的解答思路和尝试过的方法

尝试转换MySQL数据类型,无效

我想要达到的结果

phone和sex字段可以由JDBC技术正常插入数据

  • 写回答

3条回答 默认 最新

  • CSDN专家-微编程 2021-11-24 15:42
    关注

    你手机号使用String的情况下,你将表里的phone类型修改一下即可,对应的sql命令是

    alter table user modify phone varchar(11) ;
    

    执行后就可以了

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

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 11月24日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?