A prepared statement only throws an exception if it is not debugged

I am using this piece of code to insert some data into a database:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:war_odbc"); PreparedStatement st = con.prepareStatement( "INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)" ); st.setString(1, "Robert"); st.setString(2, "de Niro"); st.setInt(3,45); st.executeUpdate(); con.close(); 

If I use the debugger and step by step, everything goes well. If I do not use it and just run the application, I get this exception:

  [Microsoft][ODBC Driver Manager] Invalid string or buffer length at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source) at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source) at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source) at sun.jdbc.odbc.JdbcOdbcConnection.buildTypeInfo(Unknown Source) at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source) at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) 

I am using the 64-bit version of Windows 7. I went to Administration, Data Sources (ODBC), and I successfully tested it.

+7
source share
3 answers

The best solution is to stop using this class from the sun package ( Why developers should not write programs called "sun" Packages ). Also, this driver is really old, it's a type 1 driver that converts JDBC calls to ODBC calls.

Now, almost all database vendors have implemented a Type 4 driver. This driver implementation converts JDBC calls directly to the vendor-specific database protocol.

Also, according to this documentation, this bridge will be removed in JDK8, so it is a bad idea to use it if you want a portable / adaptable solution.

0
source

Replace st.setInt(2, "de Niro"); at st.setString(2, "de Niro");

0
source

Just a few points regarding the provided code snippet.

  • st.setInt (2, "de Niro");
    Here you set "string" as the second request parameter, but you used the setInt () method, and that is not true.

  • This is related to your OS. I mean that you are using a 64-bit widow OS and a 32-bit connector. I'm not sure about that though.

Check out this link. "[Microsoft] [ODBC Driver Manager] Invalid line or buffer length" error

0
source

All Articles