Click to See Complete Forum and Search --> : retrieve error info from SQLServer
Hi
I am having difficulty retrieving errors from my DB.
I use SQL 7.0 with the Sun jdbc-odbc driver.
When I raise an error in a stored proc:
SQL code:
--
RAISERROR('This is a test error message',16,1)
--
I would expect it to get picked up by my java code:
try{
//connect to database
Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","", "");
//create and execute sql
CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
ResultSet rs = cs.executeQuery();
while (rs.next()){
System.out.println(rs.getString("text"));
}
//cleanup
rs.close();
cs.close();
cn.close();
}
catch (SQLException se){
System.out.println("SQL exception: " + se.getMessage());
}
But it just doesnt get picked up. any ideas?
adam
MarkN
12-07-2001, 05:52 AM
"adam" <adam@tenwisevirgins.com> wrote:
>
>Hi
>
>I am having difficulty retrieving errors from my DB.
>
>I use SQL 7.0 with the Sun jdbc-odbc driver.
>
>When I raise an error in a stored proc:
>
>SQL code:
>--
>RAISERROR('This is a test error message',16,1)
>--
>
>I would expect it to get picked up by my java code:
>
>try{
> //connect to database
> Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","", "");
>
> //create and execute sql
> CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
> ResultSet rs = cs.executeQuery();
>
> while (rs.next()){
> System.out.println(rs.getString("text"));
> }
>
>
> //cleanup
> rs.close();
> cs.close();
> cn.close();
> }
>
> catch (SQLException se){
> System.out.println("SQL exception: " + se.getMessage());
> }
>
>
>But it just doesnt get picked up. any ideas?
>
>adam
>
>
By picked up do you mean an SQLException is not thrown? It has been a few
years since I coded one of those in T-SQL and that was with VB. I think
that is right levels for errors. I've thrown errors with DB2 and Java in
triggers and it worked. Is the RAISEERROR the only thing in your proc?
Why are you using a stored proc? Avoid them and use them as a last resort.
The main reason people use Java(cross platform/vendor neutral) is the same
reason you shouldn't use stored procs. Sometimes they are necessary like
in triggers.
"MarkN" <mnuttall@nospam.com> wrote:
>
>"adam" <adam@tenwisevirgins.com> wrote:
>>
>>Hi
>>
>>I am having difficulty retrieving errors from my DB.
>>
>>I use SQL 7.0 with the Sun jdbc-odbc driver.
>>
>>When I raise an error in a stored proc:
>>
>>SQL code:
>>--
>>RAISERROR('This is a test error message',16,1)
>>--
>>
>>I would expect it to get picked up by my java code:
>>
>>try{
>> //connect to database
>> Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","", "");
>>
>> //create and execute sql
>> CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
>> ResultSet rs = cs.executeQuery();
>>
>> while (rs.next()){
>> System.out.println(rs.getString("text"));
>> }
>>
>>
>> //cleanup
>> rs.close();
>> cs.close();
>> cn.close();
>> }
>>
>> catch (SQLException se){
>> System.out.println("SQL exception: " + se.getMessage());
>> }
>>
>>
>>But it just doesnt get picked up. any ideas?
>>
>>adam
>>
>>
>
>By picked up do you mean an SQLException is not thrown? It has been a few
>years since I coded one of those in T-SQL and that was with VB. I think
>that is right levels for errors. I've thrown errors with DB2 and Java in
>triggers and it worked. Is the RAISEERROR the only thing in your proc?
>
>Why are you using a stored proc? Avoid them and use them as a last resort.
> The main reason people use Java(cross platform/vendor neutral) is the same
>reason you shouldn't use stored procs. Sometimes they are necessary like
>in triggers.
>
Thanks. I do mean that no SQLEXception is thorwn, or at least if it is, its
not picked up in the catch block. I have tried it with higher levels, but
16-19 are errors, and 20-25 are regarded as fatal errors (according to the
SQL book on line, which close the connection.
I use stored proc's for all my data inserts - as an extra layer of data validation
and for transactions. It seems easier to me to have a transaction handled
at the database layer than in the middle layer if both updates are within
the same database.
Sorry for the double posting - not sure who reads which newsgroups and if
this was a java problem or a sql one.
Adam
MarkN
12-07-2001, 11:45 AM
"adam" <adam@tenwisevirgins.com> wrote:
>
>"MarkN" <mnuttall@nospam.com> wrote:
>>
>>"adam" <adam@tenwisevirgins.com> wrote:
>>>
>>>Hi
>>>
>>>I am having difficulty retrieving errors from my DB.
>>>
>>>I use SQL 7.0 with the Sun jdbc-odbc driver.
>>>
>>>When I raise an error in a stored proc:
>>>
>>>SQL code:
>>>--
>>>RAISERROR('This is a test error message',16,1)
>>>--
>>>
>>>I would expect it to get picked up by my java code:
>>>
>>>try{
>>> //connect to database
>>> Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","",
"");
>>>
>>> //create and execute sql
>>> CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
>>> ResultSet rs = cs.executeQuery();
>>>
>>> while (rs.next()){
>>> System.out.println(rs.getString("text"));
>>> }
>>>
>>>
>>> //cleanup
>>> rs.close();
>>> cs.close();
>>> cn.close();
>>> }
>>>
>>> catch (SQLException se){
>>> System.out.println("SQL exception: " + se.getMessage());
>>> }
>>>
>>>
>>>But it just doesnt get picked up. any ideas?
>>>
>>>adam
>>>
>>>
>>
>>By picked up do you mean an SQLException is not thrown? It has been a
few
>>years since I coded one of those in T-SQL and that was with VB. I think
>>that is right levels for errors. I've thrown errors with DB2 and Java
in
>>triggers and it worked. Is the RAISEERROR the only thing in your proc?
>>
>>Why are you using a stored proc? Avoid them and use them as a last resort.
>> The main reason people use Java(cross platform/vendor neutral) is the
same
>>reason you shouldn't use stored procs. Sometimes they are necessary like
>>in triggers.
>>
>
>Thanks. I do mean that no SQLEXception is thorwn, or at least if it is,
its
>not picked up in the catch block. I have tried it with higher levels, but
>16-19 are errors, and 20-25 are regarded as fatal errors (according to the
>SQL book on line, which close the connection.
>
>I use stored proc's for all my data inserts - as an extra layer of data
validation
>and for transactions. It seems easier to me to have a transaction handled
>at the database layer than in the middle layer if both updates are within
>the same database.
>
>Sorry for the double posting - not sure who reads which newsgroups and if
>this was a java problem or a sql one.
>
>Adam
>
I understand about the double post. But you are lucky to get a Java question
answered, let alone a Java AND SQL server one answered within these discussions.
I am throwing these type of errors from DB2 and they are caught. But we
are using a DB2 specific driver. That might be the problem. Try throwing
the higher level error just for fun and see if it works. If you are getting
an error the catch block should catch it or the JVM will if you don't have
any outer catch blocks. Try using a SQL Server specific driver.
Back on my Soap Box - any code that references the database is part of the
database layer. All you really are doing is gaining some speed (how much
really?) and making it tough to move to some other database. Yes, some data
validation/creation must occur within the database (i.e. Unique ID creation).
As the man says "Pay me now, or pay me later."
Let me now how it goes.
Mark
(Sorry about the soap box discussion - I can't control myself)
"MarkN" <mnuttall@nospam.com> wrote:
>
>"adam" <adam@tenwisevirgins.com> wrote:
>>
>>"MarkN" <mnuttall@nospam.com> wrote:
>>>
>>>"adam" <adam@tenwisevirgins.com> wrote:
>>>>
>>>>Hi
>>>>
>>>>I am having difficulty retrieving errors from my DB.
>>>>
>>>>I use SQL 7.0 with the Sun jdbc-odbc driver.
>>>>
>>>>When I raise an error in a stored proc:
>>>>
>>>>SQL code:
>>>>--
>>>>RAISERROR('This is a test error message',16,1)
>>>>--
>>>>
>>>>I would expect it to get picked up by my java code:
>>>>
>>>>try{
>>>> //connect to database
>>>> Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","",
>"");
>>>>
>>>> //create and execute sql
>>>> CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
>>>> ResultSet rs = cs.executeQuery();
>>>>
>>>> while (rs.next()){
>>>> System.out.println(rs.getString("text"));
>>>> }
>>>>
>>>>
>>>> //cleanup
>>>> rs.close();
>>>> cs.close();
>>>> cn.close();
>>>> }
>>>>
>>>> catch (SQLException se){
>>>> System.out.println("SQL exception: " + se.getMessage());
>>>> }
>>>>
>>>>
>>>>But it just doesnt get picked up. any ideas?
>>>>
>>>>adam
>>>>
>>>>
>>>
>>>By picked up do you mean an SQLException is not thrown? It has been a
>few
>>>years since I coded one of those in T-SQL and that was with VB. I think
>>>that is right levels for errors. I've thrown errors with DB2 and Java
>in
>>>triggers and it worked. Is the RAISEERROR the only thing in your proc?
>>>
>>>Why are you using a stored proc? Avoid them and use them as a last resort.
>>> The main reason people use Java(cross platform/vendor neutral) is the
>same
>>>reason you shouldn't use stored procs. Sometimes they are necessary like
>>>in triggers.
>>>
>>
>>Thanks. I do mean that no SQLEXception is thorwn, or at least if it is,
>its
>>not picked up in the catch block. I have tried it with higher levels, but
>>16-19 are errors, and 20-25 are regarded as fatal errors (according to
the
>>SQL book on line, which close the connection.
>>
>>I use stored proc's for all my data inserts - as an extra layer of data
>validation
>>and for transactions. It seems easier to me to have a transaction handled
>>at the database layer than in the middle layer if both updates are within
>>the same database.
>>
>>Sorry for the double posting - not sure who reads which newsgroups and
if
>>this was a java problem or a sql one.
>>
>>Adam
>>
>
>I understand about the double post. But you are lucky to get a Java question
>answered, let alone a Java AND SQL server one answered within these discussions.
>
>I am throwing these type of errors from DB2 and they are caught. But we
>are using a DB2 specific driver. That might be the problem. Try throwing
>the higher level error just for fun and see if it works. If you are getting
>an error the catch block should catch it or the JVM will if you don't have
>any outer catch blocks. Try using a SQL Server specific driver.
>
>Back on my Soap Box - any code that references the database is part of the
>database layer. All you really are doing is gaining some speed (how much
>really?) and making it tough to move to some other database. Yes, some
data
>validation/creation must occur within the database (i.e. Unique ID creation).
>As the man says "Pay me now, or pay me later."
>
>Let me now how it goes.
>Mark
>(Sorry about the soap box discussion - I can't control myself)
>
Thanks again. Well tried higher error levels. Also tried running the SP from
within the SQL query analyser to discover that anything higher than level
18 can only be thrown by a memeber or sysadmin and requires a write to the
log. Dont have another driver to hand at present but will play with one later.
Well maybe I made the wrong decision to use sp's but that was some time ago,
and since they exist I dont really want to have to rework it all not to.
Perhaps Ill think differently next time
Adam
PS Its only the errors thrown from within sp's that dont get caught. I cant
plenty of them if sent the DB duff SQL statements.
Adam
"adam" <adam@tenwisevirgins.com> wrote:
>
>PS Its only the errors thrown from within sp's that dont get caught. I cant
>plenty of them if sent the DB duff SQL statements.
>
>Adam
SPs are the right choice for data layer manipulations and should translate
well to any ANSI compliant dB. They are fast and secure and all in the same
place, rather than sprinkled about the Biz tier.
As for SQL exceptions, ODBC/ADO errors are returned in a collection, so you
must march through it to find your exception. (Not sure about the JDBC driver...)I
usually use the return code to signal sucess or failure, with ZERO returned
if all is well.
jwr4
"MarkN" <mnuttall@nospam.com> wrote:
>
>"adam" <adam@tenwisevirgins.com> wrote:
>>
>>Hi
>>
>>I am having difficulty retrieving errors from my DB.
>>
>>I use SQL 7.0 with the Sun jdbc-odbc driver.
>>
>>When I raise an error in a stored proc:
>>
>>SQL code:
>>--
>>RAISERROR('This is a test error message',16,1)
>>--
>>
>>I would expect it to get picked up by my java code:
>>
>>try{
>> //connect to database
>> Connection cn = DriverManager.getConnection("jdbc:odbc:TWVPAT","", "");
>>
>> //create and execute sql
>> CallableStatement cs = cn.prepareCall("{call sp_get_test_data}");
>> ResultSet rs = cs.executeQuery();
>>
>> while (rs.next()){
>> System.out.println(rs.getString("text"));
>> }
>>
>>
>> //cleanup
>> rs.close();
>> cs.close();
>> cn.close();
>> }
>>
>> catch (SQLException se){
>> System.out.println("SQL exception: " + se.getMessage());
>> }
>>
>>
>>But it just doesnt get picked up. any ideas?
>>
>>adam
>>
>>
>
>By picked up do you mean an SQLException is not thrown? It has been a few
>years since I coded one of those in T-SQL and that was with VB. I think
>that is right levels for errors. I've thrown errors with DB2 and Java in
>triggers and it worked. Is the RAISEERROR the only thing in your proc?
>
>Why are you using a stored proc? Avoid them and use them as a last resort.
> The main reason people use Java(cross platform/vendor neutral) is the same
>reason you shouldn't use stored procs. Sometimes they are necessary like
>in triggers.
>
Do not do the Try on the connection, do it on the prepared statement, dam
it.
MarkN
12-17-2001, 08:02 AM
"jwr4" <jwr4@dpsinc.com> wrote:
>
>"adam" <adam@tenwisevirgins.com> wrote:
>>
>>PS Its only the errors thrown from within sp's that dont get caught. I
cant
>>plenty of them if sent the DB duff SQL statements.
>>
>>Adam
>
>SPs are the right choice for data layer manipulations and should translate
>well to any ANSI compliant dB. They are fast and secure and all in the same
>place, rather than sprinkled about the Biz tier.
Having used 3 of the major PC databases I can say that SPs are DB specific
and seldom, if ever, translate. For this reason, if you want your app to
be database neutral, they should be used sparingly. If you don't allow users
direct access to the database they are no more secure than SQL outside the
DB.
I agree that they shouldn't be in the Business layer. But they don't have
to be SP's to be in the data layer.
ADO would return an Err.Number > 0 and/or go to a GOTO error tag.
JDBC should throw an exception. If it doesn't, there is nothing to look
at. Because an ODBC driver (not the best thing) is being used with SQL Server
(not the best thing times 2) via JDBC, me thinks this is why no exception
is being thrown. (To translate this to ADO - it is like using the standard
ODBC provider instead of the SQL Server Provider).
Mark
>
>As for SQL exceptions, ODBC/ADO errors are returned in a collection, so
you
>must march through it to find your exception. (Not sure about the JDBC driver...)I
>usually use the return code to signal sucess or failure, with ZERO returned
>if all is well.
>
>jwr4
MarkN
12-17-2001, 08:08 AM
First, pardon your French. Second, he is. Third, do you mean "only on the
prepared statement"? It won't help solve the problem.
Mark
"jh" <jh@jh.com> wrote:
>
>Do not do the Try on the connection, do it on the prepared statement, dam
>it.
devx.com
Copyright Internet.com Inc. All Rights Reserved