私は、htmlページのsubmitボタンの値を比較することにより、データベースに対してさまざまな機能(作成、読み取り、更新、削除)を実行するサーブレットを作成しようとしています。その値は送信ボタンの値に割り当てられます。 HTMLファイル:エラー単一のサーブレットを使用してcrudページを作成するとき
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HelloServlet" method="post">
<input type="text" placeholder="ID" name="id">
<input type="text" placeholder="NAME" name="name">
<input type="text" placeholder="SALARY" name="salary">
<input type="submit" value="add" name="submit">
<input type="submit" value="view" name="submit">
<input type="submit" value="update" name="submit">
</form>
</body>
</html>
JavaFile(サーブレット):
package com.gaurav;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
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 org.apache.jasper.tagplugins.jstl.core.Out;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String url="jdbc:oracle:thin:@localhost:1521:xe";
private String user="system";
private String pass="inception";
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Set response content type
// Actual logic goes here.
//String name=request.getParameter("name");
//out.println(name);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
int salary=Integer.parseInt(request.getParameter("salary"));
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e){
out.println("class not found ");
e.printStackTrace();
}
if(request.getParameter("submit").equals("add")){
try{
Connection con=DriverManager.getConnection(url,user,pass);
//Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","inception");
String query="INSERT INTO employee VALUES(?,?,?)";
PreparedStatement stm=con.prepareStatement(query);
stm.setInt(1, id);
stm.setString(2, name);
stm.setInt(3, salary);
stm.executeUpdate();
//.println(value);
out.println("Record Added");
con.close();
}
catch(SQLException e){
out.println("SQL Exception");
e.printStackTrace();
}
}
if(request.getParameter("submit").equals("view")){
try{
Connection con=DriverManager.getConnection(url,user,pass);
Statement stm=con.createStatement();
ResultSet res=stm.executeQuery("SELECT * FROM employee");
while(res.next()){
out.println(res.getString("id")+","+res.getString("name")+","+res.getString("salary"));
}
out.println("Records");
con.close();
}
catch(SQLException e){
out.println("SQL Exception");
e.printStackTrace();
}
}
if(request.getParameter("submit").equals("update")){
try{
Connection con=DriverManager.getConnection(url,user,pass);
String query="UPDATE employee SET name=?,salary=? WHERE id=?";
PreparedStatement stm=con.prepareStatement(query);
stm.setString(1, name);
stm.setInt(2, salary);
stm.setInt(3, id);
stm.executeUpdate();
//.println(value);
out.println("Record Updated");
con.close();
}
catch(SQLException e){
}
}
}
@Override
public void destroy() {
// resource release
super.destroy();
}
}
問題は、私が唯一の追加機能をperforimgていたときに、それは私にエラーを与えるものではありませんし、データがデータベースに追加されているが、ビューの機能を追加した後、私は数値書式例外を取得し始めました。 だから誰かここで問題を教えてください。 (ビューボタンをクリックした後) プログラムのスタックトレース:
SEVERE: Servlet.service() for servlet [com.gaurav.HelloServlet] in context with path [/Servlets] threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.gaurav.HelloServlet.doPost(HelloServlet.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
しかし、データベースにデータを追加する際に問題はありません –