mvkraju
asked on
JDBC query is getting executed!
Hi,
I have a query in UDB, it getting executed at the SQL command prompt.
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE('2004-04-01-00.00.0 0') GROUP BY HOUR(TSTAMP)
It's giveing result very nicely.
But the same query i am trying to execute in Java using prepareStatement. It's giving error when the query is getting executed
here is my code..
String query = "ALLMSG_HOUR_CNT","SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)"
reparedStatement pstmt = conn.prepareStatement(quer y);
pstmt.clearParameters();
String sqlDate = "2004-04-01-00.00.00";
pstmt.setString(1, sqlDate);
ResultSet rs = pstmt.executeQuery();
It's giving error in the last line
I appreciate ur help!
thanks
I have a query in UDB, it getting executed at the SQL command prompt.
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE('2004-04-01-00.00.0
It's giveing result very nicely.
But the same query i am trying to execute in Java using prepareStatement. It's giving error when the query is getting executed
here is my code..
String query = "ALLMSG_HOUR_CNT","SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)"
reparedStatement pstmt = conn.prepareStatement(quer
pstmt.clearParameters();
String sqlDate = "2004-04-01-00.00.00";
pstmt.setString(1, sqlDate);
ResultSet rs = pstmt.executeQuery();
It's giving error in the last line
I appreciate ur help!
thanks
ASKER
I changed it as you said, it's not working..
my TSTAMP format is yyyy-MM-dd-HH.mm.ss in the DB
sorry here is the exception
sgWarehouseRptAction.execu te(474) - java.lang.NullPointerExcep tion
STDOUT=TRUEjava.lang.NullP ointerExce ption
at com.cfg.ia.mwr.db.SqlHandl er.process MyQuery(Sq lHandler.j ava:103)
at com.cfg.ia.mwr.db.SqlHandl er.msgHour CountExec( SqlHandler .java:624)
at com.cfg.ia.mwr.action.MsgW arehouseRp tAction.ev aluateTarg et(MsgWare houseRptAc tion.java: 58)
at com.cfg.ia.mwr.action.MsgW arehouseRp tAction.ex ecute(MsgW arehouseRp tAction.ja va:450)
at org.apache.struts.action.R equestProc essor.proc essActionP erform(Req uestProces sor.java:4 84)
my TSTAMP format is yyyy-MM-dd-HH.mm.ss in the DB
sorry here is the exception
sgWarehouseRptAction.execu
STDOUT=TRUEjava.lang.NullP
at com.cfg.ia.mwr.db.SqlHandl
at com.cfg.ia.mwr.db.SqlHandl
at com.cfg.ia.mwr.action.MsgW
at com.cfg.ia.mwr.action.MsgW
at org.apache.struts.action.R
ASKER
By the by i observed one thing
this code is getting executed in the command prompt
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE('2004-04-01-00.00.0 0') GROUP BY HOUR(TSTAMP)
but this is not
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE("2004-04-01-00.00.0 0") GROUP BY HOUR(TSTAMP)
only the difference between the both queries is
second one date in enclosed in double quote.
this code is getting executed in the command prompt
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE('2004-04-01-00.00.0
but this is not
db2 => SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2A.MQMG WHERE DATE(TSTAMP
)=DATE("2004-04-01-00.00.0
only the difference between the both queries is
second one date in enclosed in double quote.
Ah, yeas indeed.
Change to:
String sqlDate = "2004-04-01-00.00.00";
pstmt.setBytes(1, sqlDate.getBytes());
Change to:
String sqlDate = "2004-04-01-00.00.00";
pstmt.setBytes(1, sqlDate.getBytes());
Oops, don't forget to add the single quotes...to quick with the submit...
String sqlDate = "'2004-04-01-00.00.00'";
String sqlDate = "'2004-04-01-00.00.00'";
ASKER
sorry, it did not work for me.
What doesn't work? Do you still get an error, or does the result set not contain any rows?
ASKER
When i try this
try
{
ConnHandler connHandler = new ConnHandler(false);
logger.debug("got connHandler...");
Connection conn = connHandler.getConn();
logger.debug("got connection...");
String query = "SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE "+
"DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)";
logger.debug("query |"+query);
PreparedStatement pstmt = conn.prepareStatement(quer y);
pstmt.clearParameters();
String sqlDate = "2004-04-01-00.00.00";
logger.debug("sqlDate |"+sqlDate);
pstmt.setString(1, sqlDate);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
logger.debug("it has data..");
}
rs.close();
pstmt.close();
conn.close();
}
catch(SQLException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(IllegalAccessExcepti on e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(InstantiationExcepti on e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(ClassNotFoundExcepti on e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(NamingException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
i am getting the following error
STDOUT=TRUE2004-04-08 13:53:52,991-main (DEBUG) SqlExecutor.<init>(32) - got connHandler...
STDOUT=TRUE2004-04-08 13:53:53,001-main (DEBUG) ConnHandler.getDriverConn( 101) - Entering...
STDOUT=TRUE2004-04-08 13:53:53,211-main (DEBUG) SqlExecutor.<init>(34) - got connection...
STDOUT=TRUE2004-04-08 13:53:53,211-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 13:53:53,221-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |2004-04-01-00.00.00
java.lang.UnsatisfiedLinkE rror: SQLBindChar
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec ute2(DB2Pr eparedStat ement.java :1675)
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec uteQuery(D B2Prepared Statement. java:1398)
at com.cfg.ia.mwr.test.SqlExe cutor.<ini t>(SqlExec utor.java: 43)
at com.cfg.ia.mwr.test.SqlExe cutor.main (SqlExecut or.java:77 )
at java.lang.reflect.Method.i nvoke(Nati ve Method)
at com.intellij.rt.execution. applicatio n.AppMain. main(Unkno wn Source)
STDOUT=TRUEException in thread "main" Process terminated with exit code 1
try
{
ConnHandler connHandler = new ConnHandler(false);
logger.debug("got connHandler...");
Connection conn = connHandler.getConn();
logger.debug("got connection...");
String query = "SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE "+
"DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)";
logger.debug("query |"+query);
PreparedStatement pstmt = conn.prepareStatement(quer
pstmt.clearParameters();
String sqlDate = "2004-04-01-00.00.00";
logger.debug("sqlDate |"+sqlDate);
pstmt.setString(1, sqlDate);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
logger.debug("it has data..");
}
rs.close();
pstmt.close();
conn.close();
}
catch(SQLException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(IllegalAccessExcepti
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(InstantiationExcepti
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(ClassNotFoundExcepti
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch(NamingException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
i am getting the following error
STDOUT=TRUE2004-04-08 13:53:52,991-main (DEBUG) SqlExecutor.<init>(32) - got connHandler...
STDOUT=TRUE2004-04-08 13:53:53,001-main (DEBUG) ConnHandler.getDriverConn(
STDOUT=TRUE2004-04-08 13:53:53,211-main (DEBUG) SqlExecutor.<init>(34) - got connection...
STDOUT=TRUE2004-04-08 13:53:53,211-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 13:53:53,221-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |2004-04-01-00.00.00
java.lang.UnsatisfiedLinkE
at COM.ibm.db2.jdbc.app.DB2Pr
at COM.ibm.db2.jdbc.app.DB2Pr
at com.cfg.ia.mwr.test.SqlExe
at com.cfg.ia.mwr.test.SqlExe
at java.lang.reflect.Method.i
at com.intellij.rt.execution.
STDOUT=TRUEException in thread "main" Process terminated with exit code 1
ASKER
When i try this code as per ur suggestion
onnHandler connHandler = new ConnHandler(false);
logger.debug("got connHandler...");
Connection conn = connHandler.getConn();
logger.debug("got connection...");
String query = "SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE "+
"DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)";
logger.debug("query |"+query);
PreparedStatement pstmt = conn.prepareStatement(quer y);
pstmt.clearParameters();
String sqlDate = "'2004-04-01-00.00.00'";
logger.debug("sqlDate |"+sqlDate);
// pstmt.setString(1, sqlDate);
pstmt.setBytes(1, sqlDate.getBytes());
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
logger.debug("it has data..");
}
rs.close();
pstmt.close();
conn.close();
i am gettring the follosing error
STDOUT=TRUE2004-04-08 13:59:54,380-main (DEBUG) SqlExecutor.<init>(32) - got connHandler...
STDOUT=TRUE2004-04-08 13:59:54,390-main (DEBUG) ConnHandler.getDriverConn( 101) - Entering...
STDOUT=TRUE2004-04-08 13:59:54,891-main (DEBUG) SqlExecutor.<init>(34) - got connection...
STDOUT=TRUE2004-04-08 13:59:54,891-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 13:59:54,901-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |'2004-04-01-00.00.00'
COM.ibm.db2.jdbc.DB2Except ion: [IBM][CLI Driver][DB2/6000] SQL0418N A statement contains a use of a parameter marker that is not valid. SQLSTATE=42610
at COM.ibm.db2.jdbc.app.SQLEx ceptionGen erator.thr ow_SQLExce ption(SQLE xceptionGe nerator.ja va:261)
at COM.ibm.db2.jdbc.app.SQLEx ceptionGen erator.thr ow_SQLExce ption(SQLE xceptionGe nerator.ja va:209)
at COM.ibm.db2.jdbc.app.SQLEx ceptionGen erator.che ck_return_ code(SQLEx ceptionGen erator.jav a:443)
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec ute2(DB2Pr eparedStat ement.java :1882)
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec uteQuery(D B2Prepared Statement. java:1398)
at com.cfg.ia.mwr.test.SqlExe cutor.<ini t>(SqlExec utor.java: 44)
at com.cfg.ia.mwr.test.SqlExe cutor.main (SqlExecut or.java:78 )
at java.lang.reflect.Method.i nvoke(Nati ve Method)
at com.intellij.rt.execution. applicatio n.AppMain. main(Unkno wn Source)
onnHandler connHandler = new ConnHandler(false);
logger.debug("got connHandler...");
Connection conn = connHandler.getConn();
logger.debug("got connection...");
String query = "SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE "+
"DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)";
logger.debug("query |"+query);
PreparedStatement pstmt = conn.prepareStatement(quer
pstmt.clearParameters();
String sqlDate = "'2004-04-01-00.00.00'";
logger.debug("sqlDate |"+sqlDate);
// pstmt.setString(1, sqlDate);
pstmt.setBytes(1, sqlDate.getBytes());
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
logger.debug("it has data..");
}
rs.close();
pstmt.close();
conn.close();
i am gettring the follosing error
STDOUT=TRUE2004-04-08 13:59:54,380-main (DEBUG) SqlExecutor.<init>(32) - got connHandler...
STDOUT=TRUE2004-04-08 13:59:54,390-main (DEBUG) ConnHandler.getDriverConn(
STDOUT=TRUE2004-04-08 13:59:54,891-main (DEBUG) SqlExecutor.<init>(34) - got connection...
STDOUT=TRUE2004-04-08 13:59:54,891-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)=DATE(?) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 13:59:54,901-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |'2004-04-01-00.00.00'
COM.ibm.db2.jdbc.DB2Except
at COM.ibm.db2.jdbc.app.SQLEx
at COM.ibm.db2.jdbc.app.SQLEx
at COM.ibm.db2.jdbc.app.SQLEx
at COM.ibm.db2.jdbc.app.DB2Pr
at COM.ibm.db2.jdbc.app.DB2Pr
at com.cfg.ia.mwr.test.SqlExe
at com.cfg.ia.mwr.test.SqlExe
at java.lang.reflect.Method.i
at com.intellij.rt.execution.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
here is the query response
STDOUT=TRUE2004-04-08 16:34:23,649-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)= CAST(? AS DATE) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 16:34:23,659-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |2004-04-01-00.00.00
java.lang.UnsatisfiedLinkE rror: SQLBindChar
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec ute2(DB2Pr eparedStat ement.java :1675)
at COM.ibm.db2.jdbc.app.DB2Pr eparedStat ement.exec uteQuery(D B2Prepared Statement. java:1398)
STDOUT=TRUE2004-04-08 16:34:23,649-main (DEBUG) SqlExecutor.<init>(37) - query |SELECT HOUR(TSTAMP), COUNT(MSGID) FROM DB2admin.MQMDLOG WHERE DATE(TSTAMP)= CAST(? AS DATE) GROUP BY HOUR(TSTAMP)
STDOUT=TRUE2004-04-08 16:34:23,659-main (DEBUG) SqlExecutor.<init>(41) - sqlDate |2004-04-01-00.00.00
java.lang.UnsatisfiedLinkE
at COM.ibm.db2.jdbc.app.DB2Pr
at COM.ibm.db2.jdbc.app.DB2Pr
ASKER
orangehead911 thanks for your support.
This query is not working with PreparedStatement.
But it works with Statement. I don't know what is the reason.
But i could keep moving on.
Thanks
This query is not working with PreparedStatement.
But it works with Statement. I don't know what is the reason.
But i could keep moving on.
Thanks
I think you'll be better off just sticking with Statement for now. Another option of course is to have the statement as a stored procedure in the database and access it with the CallableStatement instead.
String sqlDate = "2004-04-01-00.00.00"; ==> String sqlDate = "2004-04-01 00:00:00";
P.s. always include the errors in your questions d.s.