import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.lob.LobHandler;
class Main {
public static void main(String args[]) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
// DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
final LobHandler lobHandler = (LobHandler) ac.getBean("lobHandler");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.query("select comments from t_customer where id=?", new Object[] { 2L },
new RowMapper() {
private String readAsAscii(InputStream is) throws IOException {
StringBuffer out = new StringBuffer();
byte[] buffer = new byte[1024];
int read;
try {
while ((read = is.read(buffer, 0, buffer.length)) > 0) {
for (int i = 0; i < read; i++) {
out.append(String.format("%x", buffer[i]));
}
}
} finally {
is.close();
}
return out.toString();
}
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
InputStream stream = lobHandler.getClobAsAsciiStream(rs, 1);
try {
return readAsAscii(stream);
} catch (IOException ex) {
//
}
return null;
}
});
}
}
|