Jdbc driver with SQL Server
i have used type 2 driver for connectin to my SQL Server. Its working fine with normal java program. but when i used the same driver in my servlet its returning null pointer exception. i.e. Connection is null
here is the code i have used:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn =DriverManager.getConnection("jdbc:odbc:mydsn",
"username",
"password");
That is THE type 1 driver by the way.
Anyway who knows. Have you considered using a type 4 (pure Java driver) instead? There are several available that work with SQL server including one from microsoft (not recommended) and one from sourceforge http://jtds.sourceforge.net/ (recommended)
I would strongly consider using a type 4 driver instead because the problem likely has something to do with it can't find the DSN or the bridge is not available on your servlet platform etc etc
FInally if your connection is null it suggests that you are swallowing an exception somewhere because DriverManager.getConnection will throw an exception if it can't connect (which appears to be the case).
is the database running on the same machine as the web container? if not, the type 2 driver certainly won't worki agree, use type 4over the past 8 years, that's all i've seen used for web apps anyway
> Hi
> no my database server not on the same machine. but
> ut the same driver is working with a simple java
> program and not working with servlet.
Did you set up the ODBC dsn on the machine where the servlet is running. You have to do that with that driver.
Use the type 4 driver.
1.Install the SQL Server 2000 Driver for JDBC Service Pack 3. The SQL Server JDBC driver JAR files msbase.jar, mssqlserver.jar and msutil.jar get installed in the C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib directory.
2. Specify the driver class com.microsoft.jdbc.sqlserver.SQLServerDriver
3. Specify connection url jdbc:microsoft:sqlserver://localhost:1433