can't load jdbc driver in servlet
843854Feb 15 2003 — edited Feb 17 2003Dear All,
I have tried to connect to MS SQL SERVER 2000 through JDBC DRIVER FOR
SQLSERVER. However I receieved an error: can't load jdbc driver class:
null.
The following steps were what I did:
1.Set up SQL Server and create a few tables and users.
2.Config Server.xml
Add the following codes in /tomcat/conf/Server.xml
<Context path="/persistence" docBase="persistence" debug="0"
reloadable="true">
<Resource name="jdbc/persistenceDB" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/PersistenceDB">
<Parameter>
<name>user</name>
<value>kitty</value>
</Parameter>
<Parameter>
<name>password</name>
<value>abc</value>
</Parameter>
<Parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</Parameter>
<Parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;databaseName=persistence;selectMethod=cursor;</value>
</Parameter>
</ResourceParams>
</Context>
3.Config my web application's web.xml
Added the following piece of code in WEB.XML
<resource-ref>
<res-ref-name>jdbc/persistenceDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.Use Datasource in my code
package persistence.database;
import java.sql.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.servlet.ServletContext;
import persistence.*;
import com.microsoft.jdbcx.sqlserver.*;
import javax.naming.Context;
public class DatabaseAuthenticator implements Authenticator {
private DataSource ds;
public void init(ServletContext sctx) throws Exception {
InitialContext init = new InitialContext();
try{
Context ctx = (Context) init.lookup("java:/comp/env");
ds = (DataSource) ctx.lookup("jdbc/persistenceDB");
} catch (Exception e) { e.printStackTrace();
}
}
public User authenticate(String username, String password)
throws AuthenticationException, UnknownException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs;
try {
if (ds != null) {System.out.println("Success to get
datasource");
} else {System.out.println("Failed");}
System.out.println(ds.toString());
try
{
conn = ds.getConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
pstmt = conn.prepareStatement("SELECT user_id " + .........
5. put msutil.jar, mssqlserver.jar and msbase.jar under /tomcat/common/lib/.
When I tried to run the program, I receieved an error: can't load jdbc
driver class: null.
I've got no idea what's going on.
Any Suggestion?
Thanks in advance
Kitty