Results 1 to 4 of 4

Thread: "set char_convert on with no_error" in PreparedSta

  1. #1
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    54

    Default "set char_convert on with no_error" in PreparedSta

    Hi,

    I am using the jdbcTemplate.batchUpdate() method on a Sybase database and because I am reading from a unrealiable data source, I have some bad data (outside unicode 16bit) to insert in the DB like "âæ?5B±*".

    The Sybase manual say that I need to set some properties of the database session. I can do this in executing "set char_convert on with no_error" on the PreparedStament.

    http://manuals.sybase.com/onlinebook...28381;pt=14594

    So, basically It seems that I should do:
    Code:
    preparedStatement.execute("set char_convert on with no_error");
    But when I insert this code in the place I guess it should be, I get this error:

    Code:
    org.springframework.jdbc.UncategorizedSQLException: (executing PreparedStatementCallback [org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator@70fe3f63]): encountered SQLException [JZ0S3: The inherited method execute(String) cannot be used in this subclass.]; nested exception is java.sql.SQLException: JZ0S3: The inherited method execute(String) cannot be used in this subclass.

    The code I use is this one. I am following the example of Spring in Action, page 148 (Listing 4.8 ):

    Code:
    	public void updateBatchRecord(String sql,  final List objList){
    
    		BatchPreparedStatementSetter setter = null;
    		
    		setter = new BatchPreparedStatementSetter(){
    			public int getBatchSize(){
    				return objList.size();
    			}
    			public void setValues(PreparedStatement ps, int index) throws SQLException {
    				ps.execute&#40;"set char_convert off"&#41;; <----- EXCEPTION HERE
    				Iterator it = &#40;&#40;List&#41;objList.get&#40;index&#41;&#41;.iterator&#40;&#41;;
    				int i=0;
    				while&#40;it.hasNext&#40;&#41;&#41;&#123;
    					i++;
    					Object obj = &#40;Object&#41;it.next&#40;&#41;;
    					ps.setObject&#40;i, obj&#41;;
    				&#125;
    			&#125;
    		&#125;;
    
    // Finally, I call the Spring DAO and do a simple
    //->JdbcTemplate jdbcTemplate = new 	JdbcTemplate&#40;dataSource&#41;;
    //->jdbcTemplate.batchUpdate&#40;sql,setter&#41;;
    SpringUtil.getSybaseDataAccess&#40;&#41;.updateBatchUpdate&#40;sql,setter&#41;;
    	&#125;
    So does anyone know how to execute a query on the PreparedStatement passed in the setValues() or elsewhere? I just need to execute "set char_convert on with no_error"before I update data in the database using the batchUpdate..


    Any idea?

    Thanks,

    Etienne.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Sounds like you need to create a separate statement. Try replacing ps.execute() with the following code snippet:
    Code:
                  Statement st = ps.getConnection&#40;&#41;.createStatement&#40;&#41;;
                  st.execute&#40;"set char_convert off"&#41;;
                  st.close&#40;&#41;;
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    54

    Default

    Thanks, but it didn't work either. It seems that the statement I got from the connection is not the same used by the batch update. I now get the classic exception of "cannot convert char...".

    Here's the code I put as you proposed me :


    Code:
    	/**
    	 * ObjList is a List &#40;list of Updates&#41; of List &#40;list of values of an update&#41;
    	 * @param vo
    	 */
    	public void updateBatchRecord&#40;String sql,  final List objList&#41;&#123;
    
    		BatchPreparedStatementSetter setter = null;
    		
    		setter = new BatchPreparedStatementSetter&#40;&#41;&#123;
    			public int getBatchSize&#40;&#41;&#123;
    				return objList.size&#40;&#41;;
    			&#125;
    			public void setValues&#40;PreparedStatement ps, int index&#41; throws SQLException &#123;
    				  Statement st = ps.getConnection&#40;&#41;.createStatement&#40;&#41;;
    	              st.execute&#40;"set char_convert off"&#41;;
    	              st.close&#40;&#41;;
    				  logger.debug&#40;"statment created"&#41;;
    				Iterator it = &#40;&#40;List&#41;objList.get&#40;index&#41;&#41;.iterator&#40;&#41;;
    				int i=0;
    				while&#40;it.hasNext&#40;&#41;&#41;&#123;
    					i++;
    					Object obj = &#40;Object&#41;it.next&#40;&#41;;
    					ps.setObject&#40;i, obj&#41;;
    				&#125;
    			&#125;
    		&#125;;		SpringUtil.getSybaseDataAccess&#40;&#41;.updateBatchUpdate&#40;sql,setter&#41;;
    	&#125;
    
    	
    	public void processRow&#40;ResultSet rs&#41; throws SQLException &#123;
    		List values = new ArrayList&#40;&#41;;
    		int cols = rs.getMetaData&#40;&#41;.getColumnCount&#40;&#41;;
    
    		for&#40;int i=0; i<cols; i++&#41;&#123;
    			values.add&#40;rs.getObject&#40;i+1&#41;&#41;;
    		&#125;
    		objList.add&#40;values&#41;;
    
    		// BATCH_NO == 100 for testing
    		if&#40;objList.size&#40;&#41;>=BATCH_NO&#41;&#123;
    			String sql = "INSERT INTO "+tableName+" VALUES ";
    			String sqlValue = "&#40;";
    			for&#40;int i=0; i<cols-1; i++&#41;&#123;
    				sqlValue += "?,";
    			&#125;
    			sqlValue += "?&#41;";
    
    // should write 100 batch to go
    			logger.debug&#40;"start batch "+objList.size&#40;&#41;&#41;;
    			updateBatchRecord&#40;sql+sqlValue, objList&#41;;
    // reset the objList Array
    			objList = new ArrayList&#40;&#41;;
    // should write 0 batch to go
    			logger.debug&#40;"end batch "+objList.size&#40;&#41;&#41;;
    		&#125;
    	&#125;
    When I run this code I get this error as the setting "set char_convert off" is not effective :

    Code:
    org.springframework.jdbc.UncategorizedSQLException&#58; &#40;executing PreparedStatementCallback &#91;org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator@5d3b1765&#93;&#41;&#58; encountered SQLException &#91;JZ0BE&#58; BatchUpdateException&#58; Error occurred while executing batch statement&#58; Error converting characters into server's character set. Some character&#40;s&#41; could not be converted.
    &#93;; nested exception is com.sybase.jdbc2.jdbc.SybBatchUpdateException&#58; JZ0BE&#58; BatchUpdateException&#58; Error occurred while executing batch statement&#58; Error converting characters into server's character set. Some character&#40;s&#41; could not be converted.
    
    com.sybase.jdbc2.jdbc.SybBatchUpdateException&#58; JZ0BE&#58; BatchUpdateException&#58; Error occurred while executing batch statement&#58; Error converting characters into server's character set. Some character&#40;s&#41; could not be converted.
    
    	at com.sybase.jdbc2.jdbc.ErrorMessage.raiseBatchUpdateException&#40;ErrorMessage.java&#58;698&#41;
    	at com.sybase.jdbc2.jdbc.SybStatement.batchLoop&#40;SybStatement.java&#58;1330&#41;
    	at com.sybase.jdbc2.jdbc.SybStatement.sendBatch&#40;SybStatement.java&#58;1139&#41;
    	at com.sybase.jdbc2.jdbc.SybStatement.executeBatch&#40;SybStatement.java&#58;1106&#41;
    	at com.sybase.jdbc2.jdbc.SybPreparedStatement.executeBatch&#40;SybPreparedStatement.java&#40;Compiled Code&#41;&#41;
    	at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch&#40;NewProxyPreparedStatement.java&#58;1722&#41;
    	at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement&#40;JdbcTemplate.java&#58;743&#41;
    	at org.springframework.jdbc.core.JdbcTemplate.execute&#40;JdbcTemplate.java&#58;450&#41;
    	at org.springframework.jdbc.core.JdbcTemplate.execute&#40;JdbcTemplate.java&#58;469&#41;
    	at org.springframework.jdbc.core.JdbcTemplate.batchUpdate&#40;JdbcTemplate.java&#58;734&#41;

    Thanks for your suggestion!


    Etienne.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Let's try something else then. How would you solve this using plain JDBC code? If you can get a straight JDBC program to work, then we should be able to get a Spring version to work as well.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •