Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java setNull for SQL type TEXT in prepared statement

Tags:

java

sql

jdbc

What java.sql.Types should I use for setting NULL in a prepared statement when the MySQL column type is TEXT? I don't see a type TEXT here: http://docs.oracle.com/javase/7/docs/api/java/sql/Types.html.

Here's my table definition (simplified):

CREATE TABLE IF NOT EXISTS `mytable` (
  `txtcolumn` text DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here's what I'd like to do:

...
java.sql.Connection c = getConnection();
PreparedStatement s = c.prepareStatement( "Insert into mytable (txtcolumn) values (?)" );
s.setNull( 0, java.sql.Types.TEXT );
...

...but there is no java.sql.Types.TEXT.

like image 282
Phillip Avatar asked Feb 06 '26 14:02

Phillip


1 Answers

You can use BLOB, CLOB, String.

All TEXT types return Types.LONGVARCHAR with different getPrecision() values (65535, 255, 16777215, and 2147483647 respectively) with getColumnType() returning -1. This behavior is intentional even though TINYTEXT does not fall, regarding to its size, within the LONGVARCHAR category. This is to avoid different handling inside the same base type. And getColumnType() returns -1 because the internal server handling is of type TEXT, which is similar to BLOB.

Also note that getColumnTypeName() will return VARCHAR even though getColumnType() returns Types.LONGVARCHAR, because VARCHAR is the designated column database-specific name for this type.

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html

like image 186
sendon1982 Avatar answered Feb 09 '26 09:02

sendon1982



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!