1

I am writing a JSP to retrieve data from the SQL server and i am getting the following error.

org.apache.jasper.JasperException: An exception occurred processing JSP page /connectiontoserver.jsp at line 10

9: 10: <% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

JSP code that i created is:

<% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433/signin");
    Statement statement = con.createStatement() ; 
ResultSet resultset = statement.executeQuery("select username from signintable") ;
%>
Lilylakshi
  • 736
  • 7
  • 19
Animesh Porwal
  • 557
  • 1
  • 10
  • 21

3 Answers3

0
<% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433/signin");
   Statement statement = con.createStatement() ; 
   ResultSet resultset = statement.executeQuery("select username from signintable") ;
%>
mabarroso
  • 659
  • 2
  • 11
  • 23
0
<html>
<body>
<%
 Connection connection = null;
    try
    {
     // the sql server driver string
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     // the sql server url
     String url = "jdbc:odbc:login";
     // get the sql server database connection
     connection = DriverManager.getConnection(url,"sa", "sql");
     Statement statement = connection.createStatement();
     ResultSet resultSet = statement.executeQuery("SELECT *FROM login");

     %>
      <table border="1">
          <tr>
              <th>Username</th>
              <th>Password</th>
          </tr>
     <%
     while ( resultSet.next() ) {
         %>

     <tr><td>
    <%
     String Username = resultSet.getString("username");
     String Password = resultSet.getString("password");
     out.println(Username);
      %>
         </td>
         <td> <%
     out.println(Password);
      }%></td>
     </tr>
      </table>
      <%
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      System.exit(2);
    }
%>
</body>
</html>

this code works for me,i m using sql server 2008 r2 ,java 1.7

simon asir
  • 49
  • 7
0

Maybe you are getting a ClassNotFoundException if it's true you need to add your MS-SQL driver to your classpath. The driver is a file with extension JAR. And for add it to you CLASSPATH you need to checj the documentation of your Web Container. If your Web Container is Tomcat you need to put it on CATALINA_HOME/lib where CATALINA_HOME is the folder where your TOMCAT is installed.

Ernesto Campohermoso
  • 7,213
  • 1
  • 40
  • 51
  • As you told i copied the JAR fine in the 'lib' folder and then specified that path in the CLASSPATH environment variable but i am still getting the same problem. Can you help me out? And yeah i am getting ClassNotFoundException. Stacktrace is as below: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) – Animesh Porwal Dec 20 '11 at 04:06
  • Hey @ernesto thanks for the help. Actually i was using wrong version of driver which was not supported by JRE. I will proceed now. Will contact you again if i get stuck. Cheers!! – Animesh Porwal Dec 20 '11 at 04:44