JDBC Null Pointer Follow
Answered
I'm trying to connect to Sybase ASE 17 via jconn4. I am able to establish a connection via the data window, but my code is failing to connect.
This code generates a null pointer
Class.forName(driverName).newInstance();
Here's the whole class - the last try/catch block fires.
public class ConnectionManager {
private static String url = "jdbc:sybase:Tds:myserverip:2025/mydb";
private static String driverName = "com.sybase.jdbc4.jdbc.SybDriver";
private static String username = "user";
private static String password = "pwd";
private static Connection con;
public static Connection getConnection() {
try {
try {
Class.forName(driverName).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
System.out.println("Drivername problem!");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
// log an exception
System.out.println("Failed to create the database connection.");
}
} catch (ClassNotFoundException ex) {
//log an exception
System.out.println("Driver not found.");
}
return con;
}
}
Thanks in advance for any suggestions!
Please sign in to leave a comment.
Make sure the jdbc driver jar where the "com.sybase.jdbc4.jdbc.SybDriver" class is in your application classpath: check that it is added to the module's dependencies and you are able to navigate to the class via Navigate | Class action.
I should have mentioned that previously I'd add the module dependency but per your suggestion I tried to find it with Navigate | Class action and was unable to. So I also added the class via Project Structure | Libraries. The name of the jar is jconn4.jar, and I can't find it still using Navigate | Class by searching for 'jconn4.' However, I am able to find 'com.sybase.jdbc4.jdbc' using Navigate | Class and it is indicating the jconn4 class. So accordingly, I changed my driver name from
'com.sybase.jdbc4.jdbc.SybDriver'
to
'com.sybase.jdbc4.jdbc'
but the error is unchanged:
Sorry, I see that I said I was using Sybase ASE 17 - actually it's Sybase ASE 15.7. Probably not at issue here, but felt compelled to make the correction :-).
Al
You may have some wrong jconn.jar file in your dependencies. I've checked that https://osdn.net/projects/sfnet_id2d/downloads/jdbc%20drivers/jconn4.jar/ works just fine. Please try it instead.
Sorry, that jar didn't make a difference. The existing jar works fine for the database window, and also for a trivial java app I wrote externally just to test it. Thus far just not programmatically in IntelliJ. It was a reasonable suggestion though!
What error do you get with the linked jar and com.sybase.jdbc4.jdbc.SybDriver class name? At least this class can be navigated in this jar, while you said you can't find it in your jar.
Same, the nullpointer on line 23. My expanded class tree structure.seems a bit different than yours - although the SybDriver class looks very similar.
What's on line 23? In order to help you we need your code.
Sorry, it was included in my original post (but no line numbers.)
Please share the complete project to reproduce the problem (zip the sample project root directory and upload it here: https://intellij-support.jetbrains.com/hc/articles/206869619).
Thanks, it's uploaded as CommitteeList2.zip
Your application is a web application deployed on Tomcat. Artifact (exploded web) that you've configured doesn't include jconn4.jar. Please update the artifact configuration to include the JDBC driver in the WEB-INF/lib directory. See https://www.jetbrains.com/help/idea/artifacts.html.
That did the trick, thanks so much for your help!