I use jdk 1.4.0 and SQL Server 2000 - 8.00.382 (Build 2195: SP1). ODBC
Driver Manager is 3.520.7326.0
This may be an issue on the sun-jdbc-driver but this may also be another
case where the behaviour of the underlying database 'is by design'.
Below is a test program that shows the behaviour. pstOK works fine, pstOdd
does not succeed. I can't believe I'm doing anything wrong. So where is the
error?
Any help would be appreciated.
Thanks
Christoph
test.java
------------------
import java.sql.*;
public class test {
public static void main (String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:odbc:test",
"sa", "");
Statement st = con.createStatement();
String sql = null;
sql = "CREATE TABLE [t_ok] ( "
+ " [id] [int] IDENTITY (1, 1) NOT NULL , "
+ " [Value] [float] NOT NULL , "
+ " CONSTRAINT [PK_t1] PRIMARY KEY CLUSTERED "
+ " ( [id] ) ON [PRIMARY] "
+ ") ON [PRIMARY] ";
st.execute(sql);
sql = "CREATE TABLE [t_odd] ( "
+ " [id] [int] IDENTITY (1, 1) NOT NULL , "
+ " [Value] [float] NOT NULL , "
+ " [id2] [int] NOT NULL , "
+ " CONSTRAINT [PK_t2] PRIMARY KEY CLUSTERED "
+ " ( [id] ) ON [PRIMARY] "
+ ") ON [PRIMARY] ";
st.execute(sql);
st.close(); st = null;
PreparedStatement pstOK = con.prepareStatement("INSERT INTO t_ok
(value) values (?)");
pstOK.setDouble(1,12.3);
pstOK.addBatch();
pstOK.executeBatch();
pstOK.close(); pstOK = null;
System.out.println("just as I expected...");
PreparedStatement pstOdd = con.prepareStatement("INSERT INTO
t_odd (value, id2) values (?,?)");
pstOdd.setDouble(1,12.3);
pstOdd.setLong(2,1);
pstOdd.addBatch();
pstOdd.executeBatch();
/* this is the output of the above statement
java.sql.SQLException: Object type and JDBC SQL type mismatch.
java.lang.Float
at
sun.jdbc.odbc.JdbcOdbcPreparedStatement.setObject(JdbcOdbcPreparedStatement.
java:1193)
at
sun.jdbc.odbc.JdbcOdbcPreparedStatement.setObject(JdbcOdbcPreparedStatement.
java:1077)
at
sun.jdbc.odbc.JdbcOdbcPreparedStatement.emulateExecuteBatch(JdbcOdbcPrepared
Statement.java:1852)
at
sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeBatchUpdate(JdbcOdbcPreparedS
tatement.java:1530)
at
sun.jdbc.odbc.JdbcOdbcStatement.executeBatch(JdbcOdbcStatement.java:884)
*/
System.out.println("what? an SQLException? And why can't I catch
it?");
// so this eventually fails
pstOK.close(); pstOK = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}