trying to update mysql driver to get rid of java.s

2019-09-07 10:55发布

问题:

I am using Prepared Statement like the following :

List<Integer> ids = new ArrayList<Integer>();

ids.add(SelectQueueRS.getInt("DTSId_int"));

PreparedStatement updateOriginal = connMain.prepareStatement(
"UPDATE test.selectiontable SET DTSStatusType_ti = 3, Queued_DialerIP_vch = ?" +
"WHERE DTSId_int IN (?)");


updateOriginal.setString(1, CurrRemoteIPAddress);
updateOriginal.setArray(2,connMain.createArrayOf("string", ids.toArray()));
updateOriginal.executeUpdate();

Getting the error on line :

updateOriginal.setArray(2,connMain.createArrayOf("string", ids.toArray()));

Here is my stack trace:

Exception in thread "main" java.sql.SQLFeatureNotSupportedException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at com.mysql.jdbc.SQLError.notImplemented(SQLError.java:1329)
at com.mysql.jdbc.JDBC4Connection.createArrayOf(JDBC4Connection.java:56)
at project.Project.main(Project.java:295)

I have gone through some old Stackoverfolow posts discussing about the same issues here where many people have suggested not to use createArrayOf method.

1) I am wondering, if I can update my JDBC driver so that I don't have to change any method in my code?But before that, I will have to check in NEtbeans, which version I am currently using. Any idea how to check that?

2) If not above step, please advise what changes I can do?

Thanks

Some posts I have referred: How to rectify the 'java.sql.SQLFeatureNotSupportedException' while using the createArrayOf() method

回答1:

From the statcktrace:

Exception in thread "main" java.sql.SQLFeatureNotSupportedException

it is very clear that the feature is not supported by Driver, you are using.

  • which version I am currently using.

Using the instance of the Driver you are working with, you can find its version.

int major = driver.getMajorVersion();
int minor = driver.getMinorVersion();

But, AFAIK, as MySQL does not support custom data types, like array, changing the driver will not work.

  • If not above step, please advise what changes I can do?

Alternatively, you can prepare the query looping through the list of values and set the place holders and then set values before execute.

Example:

StringBuilder sqlSelect = new StringBuilder( 1024 );
sqlSelect.append( "UPDATE test.selectiontable " )
         .append( "SET DTSStatusType_ti = 3, ")
         .append( "Queued_DialerIP_vch = ? " )
         .append( "WHERE DTSId_int IN ( " ); 

int paramCount = ids.size();
if( paramCount > 0 ) {
  for( i = 0; i < paramCount; i++ ) {
    sqlSelect.append( ( i > 0 ? ", ?" : "?" );
  } // for each param
  sqlSelect.append( " )" );
} // if ids list is not empty

// make the prepare statement (pst) with the above sql string
PreparedStatement pst = 
    connMain.prepareStatement( sqlStatement.toString() );

// now set the parameter values in the query
int paramIndex = 1;

pst.setString(paramIndex++, CurrRemoteIPAddress);

if( paramCount > 0 ) {
  for( i = 0; i < paramCount; i++ ) {
    pst.setLong( paramIndex++, 
                 ( (Integer)ids.get( i ) ).intValue() );
  } // for each param
} // if ids list is not empty

pst.executeUpdate();


标签: java mysql jdbc