0

I have a stored procedure which prints a simple string:

create proc proc1  
as  
print 'Hello World !'

I have an android application which uses servlet (written in Java) for making connection to SQL server Data base. The next code is inside the servlet:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1 = DriverManager.getConnection("Jdbc:Odbc:abc");
CallableStatement cstmt=con1.prepareCall("{call proc1}");
cstmt.execute();

It works fine. The procedure is being activated but now, I want to get the print message 'Hello world !' but the following code doesn't work, and SQLWarning always gets null:

SQLWarning warning = cstmt.getWarnings();
while (warning != null)
{
   System.out.println(warning.getMessage());
   warning = warning.getNextWarning();
}    

My question is how can I get thess print messages? P.S Using raiserror instead of print didn't work as well. I know already about the option of output parameters (I investigated this subject for almost a week). With your permission I would like to ask for another option.

Eliran
  • 59
  • 1
  • 8

1 Answers1

0

I'm not sure how to implement it in Java. However, as this article (not the accepted answer) hints the print works differently and doesn't produce "warning"s. You can consider using raiserror instead. As well, this Blog article describes how to use SqlInfoMessageEventHandler to get information messages.

Community
  • 1
  • 1
Leo Y
  • 659
  • 7
  • 22
  • Hi, thanks but I already know how to do it in C#. SqlInfoMessageEventHandler doesn't exist in java. About raiserror, I've already tried it. Didn't work. The SQLWarning object keeps getting null all the time. – Eliran Apr 25 '15 at 09:30
  • According to SQL Server docs, using raiserror with severity level lower than 10 is treated as warning. When you was testing raiserror, did you set a proper severity level? – Leo Y Apr 25 '15 at 10:19
  • Well, I tried it now with level 6: It is still null. Wilth level 16 by the way there is an Exception which I don't want. – Eliran Apr 25 '15 at 10:33
  • As it's said, severity greater than 10 causes exception and lower severity is treated as a warning. I'm afraid that either JDBC doesn't implement this feature of SQL server or Java integration with ODBC driver doesn't provide this functionality. An alternative can be implementing output into log table and query for the table content. – Leo Y Apr 25 '15 at 10:51
  • Yea, I really hope I will not have to use output parameters, because I have a lot of stored procedures, and each one calls few other stored procedures, so it means in every line of stored procedure which contains "Exec" of another procedure, I will have to pass an output parameter. It is really strange for me that basic thing like getting the print messages is not exist. – Eliran Apr 25 '15 at 11:13
  • I don't remember how, but there is a way to log to the file, I think. Another one is as I mentioned, to append records into a log table. Then you can read from the table.... – Leo Y Apr 25 '15 at 18:16