gpt4 book ai didi

java - 服务器(jsp)无法将数据发送到 MYSQL 数据库,因为无法识别的内部错误未显示在 stackTrace 中

转载 作者:行者123 更新时间:2023-11-29 21:19:46 26 4
gpt4 key购买 nike

我创建了一个Web项目,需要将用户数据注册到数据库表中以供进一步使用。

注册.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title> Registration</title>
<link rel="stylesheet" href="style.css">
<script language="JavaScript" type="text/javascript">
function validateform(theForm) {
var First_Name = theForm.First_Name;
var Last_Name = theForm.Last_Name;
var UserName = theForm.UserName;
var phoneNo = theForm.phoneNo;
var password = theForm.password;
var reenter_password = theForm.reenter_password;
var email = theForm.email;
var gender = theForm.gender;
var title = theForm.title;
var day = theForm.dobday;
var month = theForm.dobmonth;
var year = theForm.dobyear;
var atposition = email.value.indexOf("@");
var dotposition = email.value.lastIndexOf(".");

if (First_Name.value == "") {
alert("Name can't be empty");
First_Name.focus();
return false;
}
if (!First_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
First_Name.focus();
return false;
}
if (Last_Name.value == "") {
alert("Name can't be empty");
Last_Name.focus();
return false;
}
if (!Last_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
Last_Name.focus();
return false;
}

/* user name validation */
if (UserName.value == "") {
alert("user Name can't be empty");
UserName.focus();
return false;
}

/* email validation */
if (email.value == "") {
alert("please enter your email");
email.focus();
return false;
}
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.value.length) {
alert("Please enter a valid e-mail address \n atpostion:" + atposition + "\n dotposition:" + dotposition);
email.focus();
return false;
}

/* password validation */
if (password.value == "") {
alert("password can't be empty");
password.focus();
return false;
}
if (password.value.length < 6) {
alert("Password must be at least 6 characters long.");
password.focus();
return false;
}
if (reenter_password.value == "") {
alert("please re-enter the password");
reenter_password.focus();
return false;
}
if (!password.value.match(reenter_password.value)) {
alert("password must be same!");
return false;
}

if (!gender[0].checked && !gender[1].checked) {
alert("please select your gender");
gender.focus();
return false;
}
return true;
}
</script>
</head>
<body id="html">
<center>
<br><br>
<section class="registration_form" >
<form name="registration" method="post" action="registerUser.jsp"
onsubmit="return validateform(this);">
<table id="tab1" bgcolor="#ffffff" width="60%" height="320%">
<td width="10%"></td><td><h1>Register here</h1></td>
<tr><td width="20%"></td><td><label for="First Name">First Name</label>
<input type="First Name" id="usernametxtbox" name="First_Name" placeholder="enter First Name" >
</td></tr>
<tr><td width="20%"></td><td><label for="Last Name">Last Name</label>
<input type="Last Name" id="usernametxtbox" name="Last_Name" placeholder="enter Last Name" >
</td>
</tr>
<tr><td width="20%"></td><td>
<label for="User Name">User Name</label>
<input type="User Name" id="txtbox" name="UserName" placeholder="enter User Name" ></td>
</tr>
<tr><td width="20%"></td>
<td><label for="usermail">Email</label>
<input type="email" id="txtbox" name="email" placeholder="yourname@email.com" ></td></tr>
<tr><td width="20%"></td><td>
<label for="password">Password</label>
<input type="password" id="txtbox" name="password" placeholder="password" ></td></tr>
<tr><td width="20%"></td><td><label for="reenter_password">re-enter Password</label>
<input type="password" id="txtbox" name="reenter_password" placeholder="Re-enter password"></td></tr>
<tr><td width="20%"></td><tr height="10%"><td></td></tr>
<tr><td width="20%"></td><td><label>Date of Birth:</label>
<select name=birthday>
<option> - Month - </option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay">
<option> - Day - </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="DOBYear">
<option> - Year - </option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="196\6">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>

</select></td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td><td><label>Gender:</label><input type="radio" name="gender" value="male" checked > Male
<input type="radio" name="gender" value="female"> Female</td>
</tr>
<tr><td width="20%"></td><td><input type="submit" value="register" id="butn">
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</form>
</section>
</center>
</body>
</html>

提交后转到registerUser.jsp

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page import="java.io.*" %>
<html>
<body>
<%
String firstname=request.getParameter("First_Name");
String lastname=request.getParameter("Last_Name");
String username=request.getParameter("UserName");
String email=request.getParameter("email");
String password=request.getParameter("password");
String month=request.getParameter("birthday");
String day=request.getParameter("DOBday");
String year=request.getParameter("DOBYear");
String gender=request.getParameter("gender");
String DOB=day+"-"+month+"-"+year;

String connectionURL = "jdbc:mysql://localhost:3306/demo?useSSL=false";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "admin", "admin");
statement = connection.createStatement();
String QueryString = "insert into user_details(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+firstname+"',' " +
lastname+"', '"+username+"', '"+email+"', '"+password+"',' "+DOB+","+gender+");";
statement.executeUpdate(QueryString);
%>
User data inserted successfully
<% }
catch (Exception ex) {ex.printStackTrace();}
finally{
statement.close();
connection.close();
}
%>
</body>

变量名称似乎一切都很好,检查是否不正确。包括 MYSql 连接器。服务器运行完美。

我怀疑数据库中未识别的数据类型不匹配或任何其他服务器页面错误,因为它没有出现在堆栈跟踪中,所以很难找到缺陷。

Stacktrace Image

请帮忙解决这个问题!

我在MYSQL中有表结构 Table Structure

最佳答案

我认为您在 SQL 查询中犯了一个错误

        String QueryString = "insert into user_details"+
"(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+
firstname+"',' " + lastname+"', '"+
username+"', '"+email+"', '"+password+"','"+DOB+"','"+gender+"');";

如果仍然不起作用,请更改以下代码

检查此代码是否对我有用:

   Connection connection;
Statement statement;
String driver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/demoDb";
String login = "root";
String password = "";
try {
Class.forName(driver);
connection = DriverManager.getConnection(dbURL, login,password);
statement = connection.createStatement();
} catch (SQLException e) {
connection = null;
System.out.println("drivernot found");
System.out.println("SQLException:" + e);
} catch (Exception e) {
System.out.println("Exception:" + e);
}

关于java - 服务器(jsp)无法将数据发送到 MYSQL 数据库,因为无法识别的内部错误未显示在 stackTrace 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com