Reputation: 17842
My servlet function looks like these:
CODE:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
String userName;
String passwd;
Connection conn = null;
userName = (String)request.getParameter("userName");
passwd = (String)request.getParameter("password");
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //Or any other driver
}
catch( Exception x ){
System.out.println( "Couldn’t load drivers!" );
}
try
{
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.123:1433;databaseName=test","sample","sample");
}
catch( Exception x)
{
System.out.println("Couldnot get connection");
}
}
the output goes to two catch statements.How to overcome this?
Reply as soon as possible?
Upvotes: 0
Views: 2174
Reputation: 2968
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.123:1433;databaseName=test", "sample", "sample");
} catch (ClassNotFoundException e) {
System.out.println( "Couldn’t load drivers!" );
} catch (SQLException e) {
System.out.println("Couldnot get connection");
}
or
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.123:1433;databaseName=test", "sample", "sample");
} catch (Exception e) {
if (e instanceof ClassNotFoundException) {
System.out.println( "Couldn’t load drivers!" );
} else {
if (e instanceof SQLException) {
System.out.println("Couldnot get connection");
}
}
}
Upvotes: 0
Reputation:
Are you running this from within Eclipse? It looks like you need to add a the driver JAR file to your dependencies. You can do this from the project build path settings within Eclipse (right-click the project, select Build Path -> Configure Build Path). Then under the 'Libraries' tab you can add any jars needed, such as the SQL server driver JAR file.
If you are deploying this to a Servlet container, it looks like the JAR file is missing from the WEB-INF/lib
folder. Copy it here and you should find it works.
Upvotes: 1