I am trying to connect to Sybase database from a Spring Boot application.
I am using JConnect jconn4 jdbc driver.
Login password is encrypted at Sybase server, so I am using bouncycastle cryptography API to encrypt the password.
Below is the block of code:
This code uses plain JDBC to test the connection, later I am planning to modify this to use Spring's JDBCTemplate.
Connection con = null;
try {
Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
logger.info("Loaded Sybase driver successfully.....");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
and
String url = "jdbc:sybase:Tds:<url>:<port>/<databasename>";
Properties props = new Properties();
props.put("ENCRYPT_PASSWORD", "true");
props.put("JCE_PROVIDER_CLASS", "org.bouncycastle.jce.provider.BouncyCastleProvider");
props.put("user", "username");
props.put("password", "pwd");
and
con = DriverManager.getConnection(url, props);
When Login Success into Sybase Server:
When Login failed into Sybase Server:
What I tried alreday:
1.Compared the version of jar file used by the application at runtime for the below jars.Because there are multiple versions of same jar file exists in classpath (from various dependencies).
jconn4-7.07.jar
(sybase jdbc driver).
bcprov-jdk16-1.43.ja
r (bouncycastle crypto API).
I used below code block to find the jar used by the application at runtime.
Class clazz = null;
try {
clazz = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
logger.info("BouncyCastleProvider is ... " + clazz.toString());
if (clazz != null && clazz.getProtectionDomain() != null
&& clazz.getProtectionDomain().getCodeSource() != null) {
URL codeLocation = clazz.getProtectionDomain().getCodeSource().getLocation();
logger.info("BouncyCastleProvider jar is ... " + codeLocation.toString());
}
} catch (ClassNotFoundException e) {
logger.info(e.getMessage());
}
2.Found 4 versions of bcprov-jdk1x-x.xx.jar file through pom.xml's Dependency Hierarchy window in eclipse and 'excluded' three of those files in pom. This is to avoid version conflict of jar files.
but it is not working :-(.
Why it is able to connect within Eclispe and not while running as WAR?
Any help/direction would be much useful.
After a lot of research and spent almost 10 hrs of effort, I am able to solve this by adding 'CHARSET' into connection properties.
props.put("CHARSET", "iso_1");
Moral of the story: Problems may be bigger but solutions aren't :-).