【问题标题】:Inserting Timestamp data through jsp page and validating through servlet通过jsp页面插入时间戳数据,通过servlet验证
【发布时间】:2017-04-24 09:25:41
【问题描述】:

我正在尝试通过 JSP 页面和 servlet 将数据插入数据库。我必须存储这样的时间戳值:“2016-FEB-12 10:45:22”。当我尝试将数据输入数据库时​​,出现以下错误:“oracle.net.ns.NetException: Size Data Unit (SDU) mismatch”。

这是我的 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>

这是我的 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')\"> ");
		}
	}

}

我故意省略了数据库的用户名和密码。是否有任何特定的数据类型可用于获取时间戳值?如果有,请提出建议。任何帮助将不胜感激。

【问题讨论】:

    标签: java database jsp servlets jdbc


    【解决方案1】:

    首先考虑使用PreparedStatement(或类似名称)来避免创建 sql 插入字符串 - 这对 sql 注入攻击是开放的。

    其次,看起来time 信息应该作为TIMESTAMPDATE 插入,因此您需要使用SimpleDateFormat 将String 转换为Date

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

    搜索 SO,您可以找到数千个示例

    【讨论】:

    • 虽然你的大部分解释都很有道理,但我仍然无法弄清楚你到底想让我做什么,我是 Web 开发的新手.. 这将是一个很大的帮助如果您可以为您提到的编辑分享一些代码..
    • 如果你搜索你可以找到类似docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
    • 和设置时间mkyong.com/jdbc/…
    • 如果你能表达你的感激之情,那将是一个很大的帮助
    猜你喜欢
    • 2011-03-26
    • 2013-02-06
    • 2011-08-04
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多