0

My Java application previously ran against a SQL Server database using the Sourceforge jTds driver. It was able to use the Apache Velocity engine (v2.0) to retrieve templates from a varchar(max) column in the database. Now the database is to be encrypted so JTDS no longer works -- won't even connect successfully. I have replaced it with Microsoft JDBC driver (mssql-jdbc-7.2.2.jre8). Now the Velocity resource loader fails to find any resource, due to this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from varchar to BinaryStream is unsupported.

Tried changing the column type to text. Same error. Tried changing to image. Then Velocity apparently finds the resource but the returned template is empty.

Has anyone run into similar issues with Velocity and Microsoft JDBC?

Alperen
  • 3,772
  • 3
  • 27
  • 49

1 Answers1

0

You can configure Velocity to use your own data source resource loader :

package com.foo;

import org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader;

import java.io.Reader;
import java.io.StringReader;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyDataSourceResourceLoader extends DataSourceResourceLoader
{
    /**
     * Gets a reader from a result set's column
     * @param resultSet
     * @param column
     * @param encoding
     * @return reader
     * @throws SQLException
     */
    protected Reader getReader(ResultSet resultSet, String column, String encoding)
        throws SQLException
    {
        return new StringReader(resultSet.getString(column));
    }
}

Encodings are not checked, so they should be consistent between the application and the database.

You then tell Velocity to use your resource loader by mean of setting the following configuration properties:

resource.loaders = ds
resource.loader.ds.class = com.foo.MyDataSourceResource
Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • Thank you for your reply. This certainly would make sense. However the DataSourceResourceLoader class I'm looking at doesn't have a getReader() method to override. May I ask which version of Velocity you're assuming? I am using 1.7 (sorry I didn't specify this originally). You put me on the path to a solution in any case, however, so I thank you. – Robert Noble Oct 16 '19 at 01:48
  • @RobertNoble You say in your question: "It was able to use the Apache Velocity engine (v2.0)"... In 1.7, you'll have to override the `public synchronized InputStream getResourceStream(final String name) throws ResourceNotFoundException` method. – Claude Brisson Oct 16 '19 at 10:24