Monday, February 20, 2012

JDBC Issue with Varchar

OK..Ive successfully connected to sql server and can execute stored
procedures...if the result set is an integer it prints the result set
but has a bunch of errors:
Execute: com.microsoft.jdbc.base.BaseResultSet@.fc9944
Got from result set: 12345
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown
Source)
at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
at
com.microsoft.jdbc.base.BaseCallableStatement.getA ndValidateOutParameter(Unknown
Source)
at com.microsoft.jdbc.base.BaseCallableStatement.getS tring(Unknown
Source)
at JDBC_Test.<init>(JDBC_Test.java:24)
at JDBC_Test.main(JDBC_Test.java:31)
If the result set returns a varchar I get:
Execute: com.microsoft.jdbc.base.BaseResultSet@.fc9944
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value
can not be converted to requested type.
at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown
Source)
at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
at com.microsoft.jdbc.base.BaseData.getInteger(Unknow n Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unkno wn Source)
at JDBC_Test.<init>(JDBC_Test.java:22)
at JDBC_Test.main(JDBC_Test.java:31)
Heres my code:
import java.sql.*;
public class JDBC_Test {
private String msDbUrl =
"jdbc:microsoft:sqlserver://localhost:1433;databaseName=KB;selectMethod=cursor ;";
private java.sql.Connection mcDbAccess;
private CallableStatement msProcedure;
public JDBC_Test() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
mcDbAccess = java.sql.DriverManager.getConnection( msDbUrl,
"kbuser", "password" );
msProcedure = mcDbAccess.prepareCall("{call STP_test(?)
}");
msProcedure.setInt( 1, 1);
ResultSet lrsReturn = null;
System.out.println( "Execute: " + (lrsReturn =
msProcedure.executeQuery() ) );
while( lrsReturn.next() ) {
System.out.println( "Got from result set: " +
lrsReturn.getInt( 1 ) );
}
System.out.println( "Got from stored procedure: " +
msProcedure.getString( 1 ) );
} catch( Throwable e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JDBC_Test();
}
}
Jimbo wrote:
> OK..Ive successfully connected to sql server and can execute stored
> procedures...if the result set is an integer it prints the result set
> but has a bunch of errors:
Ok, and what were you expecting? When an ResultSet containing an
integer is returned, you process that and then you try to get a String
output parameter, which you did not declare as output parameter in the
first place (and probably it isn't a String either, as you pass an
integer as input value for that parameter).
In the second case your procedure returns a ResultSet containing a
character value and you try to read it using getInt()?
Alin.
|||I fixed it...sorry about all the questions..Im still learning this
stuff..thanks for all your help..now that I got the recordset
object...do you have a good link that shows me how to pass that
recordset object to a bean so I can view it in a jsp?
thanks
-Jim

No comments:

Post a Comment