0
我試圖通過JSP頁面和servlet將數據插入到數據庫中。我必須存儲像這樣的時間戳值:「2016-FEB-12 10:45:22」。當我嘗試將數據輸入到數據庫時,我正在引發以下錯誤:「oracle.net.ns.NetException:大小數據單元(SDU)不匹配」。通過jsp頁面插入時間戳記數據並通過servlet驗證
這是我的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 {
\t private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BidInsert() {
super();
// TODO Auto-generated constructor stub
}
\t /**
\t * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t \t
\t \t
\t \t PrintWriter out= response.getWriter();
\t \t response.setContentType("text/html");
\t \t
\t \t String bn,a,ui,li, tsi;
\t \t bn=request.getParameter("bid_no").toString();
\t \t a=request.getParameter("amount").toString();
\t \t ui=request.getParameter("u_id").toString();
\t \t li=request.getParameter("l_id").toString();
\t \t tsi=request.getParameter("timestampinfo");
\t \t
\t \t try {
\t \t \t Class.forName("oracle.jdbc.driver.OracleDriver");
\t \t Connection c=DriverManager.getConnection("jdbc:oracle:thin:@apollo.vse.gmu.edu:1521:ite10g","","");
\t \t \t String sql="insert into Bid values('"+bn+"','"+a+"','"+ui+"','"+li+"','"+tsi+"')";
\t \t \t PreparedStatement ps=c.prepareStatement(sql);
\t \t \t ps.executeUpdate(sql);
\t \t \t out.println("Data Inserted successfully");
\t \t \t
\t \t } catch (SQLException | ClassNotFoundException e) {
\t \t \t // TODO Auto-generated catch block
\t \t \t e.printStackTrace();
\t \t }
\t \t if(out.checkError()==false)
\t \t {
\t \t \t out.println(" <input type=\"button\" value=\"Check Database\" onClick=\"window.location.href('Database contents.jsp')\"> ");
\t \t }
\t }
}
我省略了用戶名&數據庫的密碼故意。是否有任何特定的數據類型用於獲取時間戳值?請提出建議。任何幫助都感激不盡。
雖然你的大部分解釋很有意義我仍然無法弄清楚你究竟要我做什麼,我是一個Web開發的新手..如果你能分享,這將是一個很大的幫助一些代碼對於你提到的編輯.. –
如果你搜索,你可以找到像http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html –
和設置時間https:// www。 mkyong.com/jdbc/how-to-insert-timestamp-value-in-preparedstatement/ –