0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I copied in the WEB-INF/lib folder but it is not showing in the eclipse.how to overcome this?
@karthik Have you tried refreshing the Eclipse project? (Right-click -> Refresh). Then you can add that JAR file to Eclipse.
0
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");
            }
        }
    }

1 Comment

your answer is not relevant to my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.