0

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

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

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

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

得到正确更新,Date但时间使用默认值更新12:00:00AM,而不是原始值。

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

JAVA代码:

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

PL/SQL 功能:


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;
buffout cursor_type; BEGIN BufferMgrXML.CLEARBUFFER; BEGIN pkg_glbl.g_login_id := login_id; pkg_glbl.g_connect:='1';
ln_dbms_cur := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(ln_dbms_cur,plsql_block,DBMS_SQL.NATIVE);
ln_cur_execute := DBMS_SQL.EXECUTE(ln_dbms_cur);
EXCEPTION WHEN BufferMgr.app_errors_exit then lreturn := BufferMgrXML.getXML(louvtBuff,1,1); RAISE EXIT_MESSAGE; END;

4

1 回答 1

2

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

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

于 2012-07-30T13:37:06.880 回答