i am getting this error messagePreparedStatement的在servlet不是從數據庫的方式獲取數據,我想
輸入形式的Input.jsp
<form class="form-horizontal" method="get"
action="${pageContext.request.contextPath}/FetchInfo"
enctype="multipart/form-data"> //using get method for servlet FetchInfo
<div class="form-group">
<label for="reg no" class="col-sm-2 control-label">Application
number</label>
<div class="col-sm-6">
<input type="number" class="form-control" name="appno" id="appno"
placeholder="Application number" max="9999">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Find</button>
</div>
</div>
</form>
的Servlet FetchInfo.java
package com.ret;
import java.io.IOException;
import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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.connection.JavaConnectDb;
import com.mysql.jdbc.Connection;
import java.sql.*;
@WebServlet("/FetchInfo")
public class FetchInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
String page = "viewdoc.jsp";
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String appno = request.getParameter("appno");
int appnumber = Integer.parseInt(appno);
String app = null;
String name = null;
String address = null;
String qual = null;
String email = null;
String cate = null;
try {
Connection conn = (Connection) JavaConnectDb.connectDb();
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(
"select Sr_no,Name,Address,Qualification,Email,Category from applications where Sr_no=?");
ps.setInt(1, appnumber);
ResultSet resultSet = ps.executeQuery();
app = resultSet.getString("Sr_no");
name = resultSet.getString("Name");
address = resultSet.getString("Address");
qual = resultSet.getString("Qualification");
email = resultSet.getString("Email");
cate = resultSet.getString("Category");
resultSet.close();
ps.close();
} catch (Exception e) {
System.out.println("Expection is;" + e);
out.print("expection e");
}
request.setAttribute("Sr_no", app);
request.setAttribute("Name", name);
request.setAttribute("Address", address);
request.setAttribute("Qualification", qual);
request.setAttribute("Email", email);
request.setAttribute("Category", cate);
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null) {
dispatcher.forward(request, response);
}
}
}
jspfile viewdoc。 jsp
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="css/bootstrap.min.css" rel="stylesheet">
<title>view documents</title>
</head>
<body>
<%
String s1 = null;
String s2 = null;
String s3 = null;
String s4 = null;
String s5 = null;
String s6 = null;
%>
<div class="table-responsive">
<table class="table table-bordered" align="center" cellpadding="5"
cellspacing="5">
<tr bgcolor="#66cc66">
<td><b>App no.</b></td>
<td><b>Name</b></td>
<td><b>Address</b></td>
<td><b>Qualification</b></td>
<td><b>Email</b></td>
<td><b>Category</b></td>
<td><b>Matriculation certificate</b></td>
<td><b>Qualification certificate</b></td>
<td><b>Category certificate</b></td>
</tr>
<%
s1 = request.getAttribute("Sr_no").toString();
int s = Integer.parseInt(s1);
s2 = request.getAttribute("Name").toString();
s3 = request.getAttribute("Address").toString();
s4 = request.getAttribute("Qualification").toString();
s5 = request.getAttribute("Email").toString();
s6 = request.getAttribute("Category").toString();
%>
<tr bgcolor="#DEB887">
<td><%=s%></td>
<td><%=s2%></td>
<td><%=s3%></td>
<td><%=s4%></td>
<td><%=s5%></td>
<td><%=s6%></td>
<td><a href="${pageContext.request.contextPath}/Matric">view</a></td>
<td><a href="${pageContext.request.contextPath}/Qual">view</a></td>
<td><a href="${pageContext.request.contextPath}/Category">view</a></td>
</tr>
</table>
</div>
<script type="text/javascript" src="js/jquery1.11.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>
*這些頁面顯示錯誤,當我使用「select * from應用程序,其中Sr_no =?」作爲PreparedStatement,用servlet FetchInfo.jsp編寫的代碼和?是從用戶輸入的jsp表單頁面input.jsp中請求的參數。 請注意,我得到了瀏覽器的輸出,當我把preparestatement作爲「選擇*從應用程序」它不工作了我當我使用的地方cluase 我使用的數據庫是Mysql 請幫助...並提前感謝整理出來...... *
您不應該直接在servlet中使用準備好的語句。這是非常糟糕的軟件架構 – Jens
添加錯誤消息,請 – Jens
所以如何使用servlet中的preparedStatements –