gpt4 book ai didi

java - 堆栈跟踪 :] with root cause

转载 作者:行者123 更新时间:2023-11-30 21:54:59 27 4
gpt4 key购买 nike

我创建了一个 JSP Mysql 和 Bootstrap Crud 应用程序。当我单击“更新”按钮时,将调用函数 validation() 来更新所有字段,在成功更新后,它将重定向到另一个 JSP 页面,所有详细信息都将被插入到 phpMyAdmin 数据库中。

但我收到了 Stacktrace:] 的根本原因 错误。还有 java.lang.NumberFormatException: null 异常。我希望你能帮助我。

代码如下:

index.jsp

 <%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.dbconnectionutil.org.DbConnection"%>
<%@page import="java.net.ConnectException"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<script src="js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
</head>
<body>
<%
Connection conn=null;

PreparedStatement ps = null;

ResultSet rs = null;

String query = "SELECT * FROM newtable";

try{

conn = DbConnection.getConnection();

ps = conn.prepareStatement(query);

rs = ps.executeQuery();


%>
<div class="container bg-info" style="padding-top:20px; padding-bottom:20px;">
<h1>Jsp Mysql And Bootstrap Crud App</h1>

<hr>

<div class="row">
<div class="col-sm-4">
<form role="form" action="addRecord.jsp" method="post">
<div class="form-group">
<label>First Name</label>
<input type ="text" class="form-control" name="fname">
</div>

<div class="form-group">
<label>Last Name</label>
<input type ="text" class="form-control" name="lname">
</div>

<div class="form-group">
<label>Email</label>
<input type ="email" class="form-control" name="email">
</div>
<button class="btn btn-info btn-block" type="submit">Add Record</button>
</form>
</div>

<div class="col-sm-8">
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Crud Actions</th>
</tr>
</thead>
<tbody>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString("fname") %></td>
<td><%=rs.getString("lname") %></td>
<td><%=rs.getString("emailid") %></td>
<td>
<a href="editform.jsp?uid<%=rs.getInt("uid") %>" class="btn btn-success" role="button">Uptade</a>
<a href="#" class="btn btn-danger" role="button">Delete</a>
</td>
</tr>
<%
}//while
}//try
catch(Exception ex){
ex.printStackTrace();
}
%>
</tbody>
</table>
</div>
</div>
</div>
</body>

</html>

editform.jsp

 <%@page import="com.dbconnectionutil.org.DbConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scaled=1">
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<script src="js/jquery.js"></script>
</head>
<body>

<%
String uid = request.getParameter("uid");

int id = Integer.parseInt(uid);

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try{
conn = DbConnection.getConnection();

String query = "SELECT * FROM newtable WHERE uid='"+id+"'";

ps = conn.prepareStatement(query);

rs = ps.executeQuery();

%>
<div class="container bg-info" style="padding-top:20px; padding-bottom:20px;">
<h1>Update Form</h1>
<hr>
<form action="" role="form">

<%
while(rs.next()){
%>
<input type="hidden" name="id" value="<%=rs.getInt("uid") %>">
<div class="form-group">
<label>First Name</label>
<input type="text" name="fname" value="<%=rs.getString("fname") %>" class="form-control">
</div>

<div class="form-group">
<label>Last Name</label>
<input type="text" name="lname" value="<%=rs.getString("lname") %>" class="form-control">
</div>


<div class="form-group">
<label>Email address</label>
<input type="email" name="email" value="<%=rs.getString("emailid") %>" class="form-control">
</div>

<%
}//while
}//try

catch(Exception ex){
ex.printStackTrace();
}
%>
<button type="submit" class="btn btn-success btn-block">Update</button>
</form>
</div>
</body>
</html>

最佳答案

您缺少关键字 =index.jsp 内元素的 href 属性中.这就是为什么你得到 NumberFormatException .

替换<a href="editform.jsp?uid<%=rs.getInt("uid") %>" >

<a href="editform.jsp?uid=<%=rs.getInt("uid") %>" >
^

当您尝试进行转换过程时,有必要捕获异常:

try{
int id = Integer.parseInt(uid);
}catch(NumberFormatException e){id = 0; } //maybe default value

重要提示:请记住@Jens 在评论中给出的建议。

关于java - 堆栈跟踪 :] with root cause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569565/

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