gpt4 book ai didi

java - 日期字段未正确更新。提交日期更新为 12 :00:00AM instead of original Value

转载 作者:行者123 更新时间:2023-12-02 07:40:09 24 4
gpt4 key购买 nike

我有一个以“日期”作为参数的存储过程。

该过程是从一个函数调用的,而该函数又是从 Java 代码调用的。

我将 Java 代码中的Date 作为参数传递。

现在的问题是存储过程没有更新数据库“日期”列中的正确时间。

Date 已正确更新,但时间更新为默认值 12:00:00AM,而不是原始值。

我使用DBMS_SQL.EXECUTE方法从函数调用存储过程。

JAVA代码:

 
import java.sql.CallableStatement;
import java.sql.ResultSet;<br/>
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;<br/>
public class StackOvFlow {<br/>
/**
*
* @param args
*/<br/>
public static void main(String[] args) {
String status = "";
try {
System.out.println("IN MAIN");<br/>
int userId = 1360822386;
StringBuffer batchUpdateSQL = new StringBuffer("BEGIN ");
StringBuffer updateSQLnew = null;
String date = "30-JUL-2012 10:30:00";<br/>
String currDate = "to_date(" + "'" + date + "'"
+ ",'DD-MON-YYYY HH24:MI:SS')";<br/>
updateSQLnew = new StringBuffer(
"BEGIN PKG_AMB_LOGIN.P_TRACK_USER("
+ userId
+ ",'"
+ currDate
+ ""
+ ");Exception When BufferMgr.app_errors_exit Then Rollback; Return; End; ");<br/>
batchUpdateSQL.append(updateSQLnew);<br/>
batchUpdateSQL
.append(" Exception When BufferMgr.app_errors_exit Then Rollback; Return; End;");<br/>
System.out.println("INPUT ::::");<br/>
System.out.println(batchUpdateSQL);<br/>
String res = getMonthlySave(batchUpdateSQL.toString());
System.out.println("Response: " + res);<br/>
} catch (Exception e) {
System.out.println("Exception" + e);
e.printStackTrace();
}
System.out.println(status);<br/>
}<br/>
public static String getMonthlySave(String plSqlBlk) {<br/>
String output = "";
System.out.println("IN GET MONTHLY SAVE");<br/>
Session session = null;<br/>
CallableStatement cstmt = null;<br/>
ResultSet rs = null;<br/>
SessionFactory sessionFactory = new Configuration().configure(
"hibernate.cfg.xml")<br/>
.buildSessionFactory();<br/>
session = sessionFactory.openSession();<br/>
Transaction transaction = session.beginTransaction();<br/>
try {<br/>
System.out<br/>
.println("<strong>*</strong><em>calling SP <strong></strong></em><strong>**</strong>");<br/>
cstmt = session.connection().prepareCall(<br/>
"{? = call PKG_THR.P_UPDT(?,?)}");<br/>
cstmt.setFetchSize(10000);<br/>
cstmt.registerOutParameter(1,<br/>
oracle.jdbc.driver.OracleTypes.CURSOR);
cstmt.setInt(2,186);<br/>
cstmt.setString(3, plSqlBlk);
cstmt.execute();<br/>
rs = (ResultSet) cstmt.getObject(1);
int size = rs.getFetchSize();<br/>
System.out.println("The size of fetch is " + size);<br/>
while (rs.next()) {<br/>
output = output + rs.getString(1);<br/>
}
System.out.println("OUTPUT ::::::::::::");<br/>
System.out.println(output);<br/>
} catch (Exception e) {<br/>
System.out<br/>
.println("Exception occured in executing query in fetchdetails....... ");<br/>
e.printStackTrace();<br/>
}<br/>
finally {<br/>
transaction.commit();<br/>
session.close();<br/>
}
return output;
}<br/>
}<p></p>

<p></p>

PL/SQL Function:

<br/>
CREATE or REPLACE
PACKAGE BODY PKG_THR AS
FUNCTION P_UPDT(login_id number,plsql_block varchar2) RETURN cursor_type IS
louvtBuff a_XMLBUFFER_TABLE;
loutRowBuff a_XMLBUFFER_TABLE := a_XMLBUFFER_TABLE();
EXIT_MESSAGE EXCEPTION;
lreturn number;
offset BINARY_INTEGER := 1;
kount BINARY_INTEGER := 5;
ln_dbms_cur NUMBER;
ln_cur_execute NUMBER;
j number := 1;
i number;<br/>
buffout cursor_type;
BEGIN
BufferMgrXML.CLEARBUFFER;
BEGIN
pkg_glbl.g_login_id := login_id;
pkg_glbl.g_connect:='1';<br/>
ln_dbms_cur := DBMS_SQL.OPEN_CURSOR;<br/>
DBMS_SQL.PARSE(ln_dbms_cur,plsql_block,DBMS_SQL.NATIVE);<br/>
ln_cur_execute := DBMS_SQL.EXECUTE(ln_dbms_cur);<br/>
EXCEPTION
WHEN BufferMgr.app_errors_exit then
lreturn := BufferMgrXML.getXML(louvtBuff,1,1);
RAISE EXIT_MESSAGE;
END;

最佳答案

因为没有太多代码可供观察,所以很难判断这种情况到底出了什么问题。

非常典型的原因是使用 java.sql.Date ,它将所有时间字段设置为 0。如果是这种情况,您可以使用 java.sql.Timestamp相反(将丢失几分之一秒)。

关于java - 日期字段未正确更新。提交日期更新为 12 :00:00AM instead of original Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com