Showing posts with label driver. Show all posts
Showing posts with label driver. Show all posts

Wednesday, March 7, 2012

Getting messages sent while JDBC Driver calls stored procedure

Hi.

How can I get the messages sent by the server while I'm executing a stored procedure via the JDBC driver?

I need to get my own debug messages (done through the print() function)

and also standard server messages (such as "x row(s) affected" or

results from SET STATISTICS TIME ON). Is this possible?

Many thanks.

Carlos

Hi Carlos,

These messages come back as warnings. You need to call Statement.getWarnings to retrieve them. See http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#getWarnings() for usage.

Hope this helps.

--David Olix

JDBC Development

|||Hi David.

That didn't work for me. I am using SQL Server 2005 JDBC driver version 1.1, and here's my calling syntax:
SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();

//(sic) set the datasource parameters...

Connection c = c.getPooledConnection().getConnection();

CallableStatement cs = c.prepareCall("{call myStoredProcedure(?,?)");

//(sic) set the stored procedure parameters...


ResultSet rs = cs.executeQuery();
SQLWarning w = cs.getWarnings();
while( w!=null ){

System.out.println(w.getMessage());

w=w.getNextWarning()

}

When debugging, I see that the first call to

cs.getWarnings() returns a null value. I also tryed rs.getWarnings()

and c.getWarnings(), and both returned null as well. When calling the

same stored

procedure through QueryAnalyser, all the messages are there.

Any ideas on what might be causing this?

Thx,

Carlos

|||

What you have should have worked if the warning message appeared before the result set. If the message came after the result set, however, you would need to process the rows in the result set before the messages can be extracted with cs.getWarnings. To be sure that you're getting through all of the results, including any warnings, you should call Statement.getMoreResults until there are no more results. See http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#getMoreResults() for how.

Thanks,

--David

getting login id in trigger

I have a web application which connects to Microsoft SQL Server 2000 through
JDBC-ODBC Driver. The application server is JBoss and I am using connection
pooling.
When the application connects to the database it provides userid and
password which are 'sa' and 'password' respectively. They are constants for
all users. The user also type in his/her own login id which I stored in the
HTTPSession.
Problem is my triggers wants to get that login id. Is it possible?
Thanks
RizwanHi
You should not be using 'sa' as the login to SQL Server as this may be too
privileged.
The users login id is only used as authentication mechanism, therefore you
will either need to pass it as part of each call to the query/stored
procedures or possibly generate some kind of session token and pass that and
then use the session token as a link to the login.
John
"Rizwan" <hussains@.pendylum.com> wrote in message
news:5Ozce.14173$gA5.818174@.news20.bellglobal.com...
>I have a web application which connects to Microsoft SQL Server 2000
>through
> JDBC-ODBC Driver. The application server is JBoss and I am using
> connection
> pooling.
> When the application connects to the database it provides userid and
> password which are 'sa' and 'password' respectively. They are constants
> for
> all users. The user also type in his/her own login id which I stored in
> the
> HTTPSession.
> Problem is my triggers wants to get that login id. Is it possible?
>
> Thanks
> Rizwan
>|||> possibly generate some kind of session token and pass that and
> then use the session token as a link to the login.
can you explain this solution a bit more about what is session token?
thanks
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:uNfEUWWTFHA.3176@.TK2MSFTNGP09.phx.gbl...
> Hi
> You should not be using 'sa' as the login to SQL Server as this may be too
> privileged.
> The users login id is only used as authentication mechanism, therefore you
> will either need to pass it as part of each call to the query/stored
> procedures or possibly generate some kind of session token and pass that
and
> then use the session token as a link to the login.
> John
>
> "Rizwan" <hussains@.pendylum.com> wrote in message
> news:5Ozce.14173$gA5.818174@.news20.bellglobal.com...
>|||Hi
The easiest way is to store and pass the user_id that the person
authenticated with within you code. You than pass this value to each
procedure that is called e.g.
EXEC myProc @.user_id = 'John'
If you want to access the user_id in a trigger you would have to add a
user_id column to each table (say last_modified_by) and set 'John' as the
value. This way you can see who changed it by accessing the last_modified_by
in the inserted table in your trigger. Alternatively you can do the work
that the trigger would have done in the stored procedure and you would not
need the extra column.
A token would be a means of relating the session to the user, if you have a
users table it may be stored in there. That way you are not passing
something that is clearly a username, but you can get the username by
selecting the appropriate record from the users table.
HTH
John
"Rizwan" <hussains@.pendylum.com> wrote in message
news:9srde.1176$3U.240756@.news20.bellglobal.com...
> can you explain this solution a bit more about what is session token?
> thanks
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message
> news:uNfEUWWTFHA.3176@.TK2MSFTNGP09.phx.gbl...
> and
>

Sunday, February 26, 2012

Getting JDBC metadata for synonyms

Using the Latest JDBC Driver from SQLExpress I'm attempting to get use the getTable() method to get information about database objects that the user can access/alter. Works fine for for tables and views, but can't seem to get any information returned for synonyms.

Is it possible to get information for synonyms, e.g. column definitions? or am I'm missing some setting in the connection.

Any Help would be appreciated!

Dave.

This a known issue and is being tracked by the SQL Server team. Can you describe the scenario for which you are usign synonyms? Are you able to owrk-around by using the table or view name?

|||Thanks for your reply,

The particular scenario we have is that we are developing a tool to create a Web Application from the database object definitions. We therefore, have no control over how the user has constructed his database and whether synonyms are used. We could get the data from the synonyms system table, but that would go against the design approach we have used. I will say that, unlike in Oracle, there seems to be no good reason to use synonyms in SQL Server at all, except possibly to enable the user to access different tables/views without changing code, which in my opinion, is a somewhat dubious practise anyway.

Dave

Getting JDBC metadata for synonyms

Using the Latest JDBC Driver from SQLExpress I'm attempting to get use the getTable() method to get information about database objects that the user can access/alter. Works fine for for tables and views, but can't seem to get any information returned for synonyms.

Is it possible to get information for synonyms, e.g. column definitions? or am I'm missing some setting in the connection.

Any Help would be appreciated!

Dave.

This a known issue and is being tracked by the SQL Server team. Can you describe the scenario for which you are usign synonyms? Are you able to owrk-around by using the table or view name?

|||Thanks for your reply,

The particular scenario we have is that we are developing a tool to create a Web Application from the database object definitions. We therefore, have no control over how the user has constructed his database and whether synonyms are used. We could get the data from the synonyms system table, but that would go against the design approach we have used. I will say that, unlike in Oracle, there seems to be no good reason to use synonyms in SQL Server at all, except possibly to enable the user to access different tables/views without changing code, which in my opinion, is a somewhat dubious practise anyway.

Dave

Friday, February 24, 2012

Getting Exception , please help !

Hi all,

I am using MSSQL Server 8.0 , and the driver jtds.jar to connect the database for SQL operation .

I can do all the sql operation .
But after the creation of a trigger for a table , I used the same java program to insert rows into table .

---The java code I used -------
String connectionString = "jdbc:jtds:sqlserver://localhost:1433/master";
try {
Class.forName(jdbcDriverClass);
Connection con = DriverManager.getConnection(connectionString,userN ame,password);
Statement st = con.createStatement();
String query = "insert into tabname values('11','ROBERT')";
st.executeUpdate(query);
} catch (Exception e) { e.printStackTrace(); }
---------------

and got the exception

////////////////////////////////////////////////////////
java.sql.SQLException: executeUpdate can't return a result set
at net.sourceforge.jtds.jdbc.TdsStatement.executeUpda te(Unknown Source)
////////////////////////////////////////////////////////

What will be the problem here ?
Is it bcos of any time out ?
Please help !!!

ThanksPost the trigger.|||CREATE TRIGGER trigName ON tabname for
INSERT , UPDATE , DELETE
AS

DECLARE @.cont varchar(255)
DECLARE @.insRsltCount int
DECLARE @.delRsltCount int
DECLARE @.newVAL_0 char(10)
DECLARE @.newVAL_1 char(10)
SELECT @.insRsltCount = COUNT(*) FROM Inserted WHERE name IS NOT NULL OR num IS NOT NULL
SELECT @.delRsltCount = COUNT(*) FROM Deleted WHERE name IS NOT NULL OR num IS NOT NULL
IF(@.insRsltCount>0) AND (@.delRsltCount=0)
BEGIN
DECLARE INS_CURSOR CURSOR FOR SELECT name, num FROM Inserted
OPEN INS_CURSOR
FETCH NEXT FROM INS_CURSOR INTO @.newVAL_0,@.newVAL_1
WHILE @.@.FETCH_STATUS = 0 AND @.insRsltCount>0
BEGIN

SELECT @.cont = ' echo tabname.name="'+LTRIM(RTRIM(@.newVAL_0))+'" >>D://MyFolder//trigName.dat'
Exec master..xp_cmdshell @.cont
SELECT @.insRsltCount = @.insRsltCount-1
FETCH NEXT FROM INS_CURSOR INTO @.newVAL_0,@.newVAL_1


END
CLOSE INS_CURSOR
DEALLOCATE INS_CURSOR
END|||The usage of xp_cmdshell has been altered with the release of SP3 (SQL2K) and SP4 (7.0). From then on the security context is being verified within the procedure which is extended stored procedure, and if a user who caused (in your case) the trigger to fire does not belong to SYSADMIN server role, - the procedure will not execute.