我有一个以'Date'作为参数的存储过程。
从一个函数调用过程,该函数又从Java代码调用。
我从Java代码中传递Date
作为参数。
现在问题是存储过程没有更新数据库'日期'列中的正确时间。
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;
答案 0 :(得分:2)
因为没有太多要观察的代码,所以很难说出这种情况究竟出了什么问题。
相当典型的原因是java.sql.Date的使用,它将所有时间字段设置为0.如果是这种情况,则可以使用java.sql.Timestamp代替(分数秒将丢失)。