Hi all,

I get 'Invalid string or buffer length' exception when trying to
do this.

String s=resultSet.GetString(1)

Exception:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3906)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
    at sun.jdbc.odbc.JdbcOdbcConnection.buildTypeInfo(JdbcOdbcConnection.java:1503)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:381)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at Controll.Test.sselect(Test.java:72)
    at Controll.Test.main(Test.java:33)

Not only getString(), any method in Resulset cannot be assigned to a variable.
But this prints the values. When i try to assign the value to a variable it throws the exception.

System.out.println(resultSet.GetString(1))

Here is my code

        try{

                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

                    Connection conn = DriverManager.getConnection("jdbc:odbc:abc");

                    Statement stmt = conn.createStatement();
                    ResultSet rss = stmt.executeQuery("Select * from TimeTrans where PID=100");

                 while (rss.next()) 
                 {
                       String s=rss.getString(1);//this lines is the problem
                      // System.out.println(rss.getString(1) );

                 }


        } catch (Exception e) {  
            e.printStackTrace();  
        }

Second column of my table is a type of varchar(10)
plz plz....help me.
Im stuck here for 2 days.

Recommended Answers

All 14 Replies

rss.getString(1)
Second column of my table is a type of varchar(10)

For some perverse reason the ResultSet get methods use indexes that are 1-based, not 0-based, so getString(1) retrieves from the first column not the second.

String s=resultSet.GetString(1)
System.out.println(resultSet.GetString(1))

The difference here is that println does an implicit toString() on whatever its parameters are, ie it's really
System.out.println(resultSet.GetString(1).toString())
whereas the simple assignment to a String variable does no such conversion

Hi,

I did this

String s =rss.getString(2).toString();

But still it throwes the exception.

If they are -1 based, then -1 is first, 0 should be second?

What do you see when you print rss.getString(2) ?

It prints the field one by one smoothly.
No probelm there. This happens when assigning.
And also as the resuls show it is 0,1,2...coz 3rd fiels is null.So it gives
Data Not Found exception. How ever now i refer to columns by their name.

    try{

            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:odbc:abc");

                Statement stmt = conn.createStatement();
                ResultSet rss = stmt.executeQuery("Select Datestamp from TimeTrans");

                 while (rss.next()) 
                 {
                      // String s =rss.getString("Datestamp").toString();
                      System.out.println(rss.getString("Datestamp") );//this prints the values

                    }


        } catch (Exception e) {  
            e.printStackTrace();  
        }

Insert statemnts work fine.

A little bit of research reveals that this is a known problem with 64bit OS and less-than-current verisons of Java and the JDBC/ODBC drivers. Are you running 64 bit?
Thye corresponding advice to update to the latest version of everything, and try to find a better JDBC driver for your database than the always slightly-dodgy JDBC/ODBC bridge.

Apparently it can also manifest with incorrect character set mappings if you are straying outside the basic ASCII characters - is that the case here?

Yes i got win 7 64bit.
I did not unserstand the last quiz.
I will update everything and post back the results.

I did not unserstand the last quiz.

There can be a problem if you have text with Greek, Arabic, Hebrew, or any other letters that are not a-z,A-Z. Java needs to know exactly how those letters are stored in the database so it can convert them to Unicode correctly.

There are no letters like that.
my java is upto date.
im updating netbeans IDE.

Is there a solution for this prob?

The next thing to try is a better JDBC driver. That JDBC/ODBC bridge has always had problems, and I believe it's been removed from Java 8.

Have you tried the latest Microsft JDBC driver for SQLServer http://msdn.microsoft.com/en-us/data/aa937724.aspx

Otherwize you can Google for alterntives

Is it posssible to get a DataSet without getting a ResultSet

I'm not familiar with DataSet. There's no such class or interface in the JavaSE 8 API.
(Was it part of the annotation-based SQL stuff that was in the 1.6 beta but never made it into the release version???)

Is it possible to install 32 bit JDBC driver in a 64bit OS?

which shows your utter lack of understanding of Java.
Your problem is your insistence on using the age old bridge driver which was never intended for being more than a technology demonstrator and inspiration for people wanting to write JDBC drivers.
It relies on 32 bit ODBC functionality which has long since been deprecated by Microsoft (a decade ago I think...).
It's only retained in the Java distribution to not make old code break that relies on it, IMO a waste of bandwidth for anyone trying to download a JDK or JRE as any code still using it past 2000 is fundamentally broken and should never be allowed to run.

Switch to another database, or find a better driver for your current one (and yes, there are some 3rd party JDBC-ODBC bridge drivers out there, maybe they're even supported still a decade after Microsoft themselves stopped supporting ODBC).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.