简体   繁体   English

通过jsp页面插入时间戳数据并通过servlet进行验证

[英]Inserting Timestamp data through jsp page and validating through servlet

I'm trying to insert data into database through a JSP page and a servlet. 我正在尝试通过JSP页面和servlet将数据插入数据库。 I have to store a timestamp value like this :"2016-FEB-12 10:45:22". 我必须存储这样的时间戳记值:“ 2016-FEB-12 10:45:22”。 When I try to enter the data into the database I'm being thrown the following error : "oracle.net.ns.NetException: Size Data Unit (SDU) mismatch". 当我尝试将数据输入数据库时​​,出现以下错误:“ oracle.net.ns.NetException:大小数据单元(SDU)不匹配”。

This is my JSP PAGE: 这是我的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>Bid Form</title> </head> <body style="background:color=LightGreen"> <form action="BidInsert" method="post"> <table border=1 align="center"> <tr> <th>Bid Number</th> <td><input type="text" name="bid_no"></td> </tr> <tr> <th>Amount</th> <td><input type="text" name="amount"></td> </tr> <tr> <th>User Id</th> <td><input type="text" name="u_id"></td> </tr> <tr> <th>Listing Id</th> <td><input type="text" name="l_id"></td> </tr> <tr> <th>Time stamp Info</th> <td><input type="text" name="timestampinfo" size=50></td> </tr> </table> <center> <input type="submit" value="submit"> </center> </form> </body> </html> 

This is my Servlet: 这是我的Servlet:

 package Serve; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class BidInsert */ public class BidInsert extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BidInsert() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out= response.getWriter(); response.setContentType("text/html"); String bn,a,ui,li, tsi; bn=request.getParameter("bid_no").toString(); a=request.getParameter("amount").toString(); ui=request.getParameter("u_id").toString(); li=request.getParameter("l_id").toString(); tsi=request.getParameter("timestampinfo"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection c=DriverManager.getConnection("jdbc:oracle:thin:@apollo.vse.gmu.edu:1521:ite10g","",""); String sql="insert into Bid values('"+bn+"','"+a+"','"+ui+"','"+li+"','"+tsi+"')"; PreparedStatement ps=c.prepareStatement(sql); ps.executeUpdate(sql); out.println("Data Inserted successfully"); } catch (SQLException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(out.checkError()==false) { out.println(" <input type=\\"button\\" value=\\"Check Database\\" onClick=\\"window.location.href('Database contents.jsp')\\"> "); } } } 

I've omitted the username & password of the database intentionally.Is there are any specific data type to be used to take timestamp values?.Please suggest if any. 我故意省略了数据库的用户名和密码,是否有任何特定的数据类型可用于获取时间戳值?请提出建议。 Any help will be much appreciated. 任何帮助都感激不尽。

For a start consider using a PreparedStatement (or similar) to avoid create sql insert strings - which are open to sql injection attacks. 首先,请考虑使用PreparedStatement (或类似方法)来避免创建sql插入字符串-这些字符串容易受到sql注入攻击。

Secondly it looks like the time info should be inserted as a TIMESTAMP or DATE , so you need to convert the String into a Date using SimpleDateFormat 其次,似乎应该将time信息作为TIMESTAMPDATE插入,因此您需要使用SimpleDateFormat将String转换为Date

see https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html 参见https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Search SO and you can find thousands of examples 搜索SO,您可以找到数千个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM