aspose file tools*
The moose likes Servlets and the fly likes Exception: com.microsoft.jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Exception: com.microsoft.jdbc" Watch "Exception: com.microsoft.jdbc" New topic
Author

Exception: com.microsoft.jdbc

anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
Hi,
i was trying to run servlet progrm,which had jdbc connectivity and i came across a runtime error:
Exception: com.microsoft.jdbc.sqlserver.SQLServerDriver

my source code is:

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class jbs extends HttpServlet {


//***** Servlet access to data base


public void doGet (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection ("jdbc:microsoft:sqlserver://160.110.243.132:1433","anubha","anubha");


Statement stmt = con.createStatement ();

ResultSet rs = stmt.executeQuery ("select * from pubs.dbo.data1");

printResultSet ( resp, rs );

rs.close();
stmt.close();
con.close();

} // end try

catch (SQLException ex) {

PrintWriter out = resp.getWriter();
resp.setContentType("text/html");

while (ex != null) {
out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException ();
} // end while

} // end catch SQLException

catch (java.lang.Exception ex) {

PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
out.println ("Exception: " + ex.getMessage ());
}

} // end doGet


private void printResultSet ( HttpServletResponse resp, ResultSet rs )
throws SQLException {

try {

PrintWriter out = resp.getWriter();

out.println("<html>");
out.println("<head><title>jbs jdbc/mysql servlet</title></head>");
out.println("<body>");
out.println("<center><font color=AA0000>");
out.println("<h3>jbsJDBCServlet</h3>");
out.println("<h3>Data Retrieved:</h3>");

out.println("<table border='1'>");

int numCols = rs.getMetaData().getColumnCount ();
while ( rs.next() ) {
out.println("<tr>");
for (int i=1; i<=numCols; i++) {
out.print("<td>" + rs.getString(i) + "</td>" );
} // end for
out.println("</tr>");
} // end while

out.println("</table>");

out.println("</font></center>");
out.println("</body>");
out.println("</html>");
out.close();

} // end try
catch ( IOException except) {
} // end catch

} // end returnHTML


} // end jbsJDBCServlet


Here data1 is the table i have created myself in the database.
Thanks in Advance.

Regards,
Anubha.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Exception: com.microsoft.jdbc.sqlserver.SQLServerDriver

That's not very helpful, and will make it very difficult to spot the problem. You should print the whole stack trace in your catch clause.


[My Blog]
All roads lead to JavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

When connecting to a database from a web application, you need to put the Driver jar file on the web application classpath, it will not be found on the system classpath.

There are a few options, but one is to place the jar in the <webapp>/WEB-INF/lib directory
anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
Hi,
i didn't understand your point as i am a newbie to java.
please tell me in detail.

Thanks & Regards,
Anubha.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Two points were made.
Which didn't you understand; David's or Christophe's?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Where did you put the Microsoft JDBC driver?
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Some possible problems,

1>Where is your sql server driver's jar file. Is under WEB-INF/lib?

2>Is a server running on the ip, you have specified in the url?

3>Wont a DatabaseName="<your db name>" required in the url after the port number?

Give the stack trace, it will help us understand the problem.
anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
Hi,
i didn't understand either of them..my sql jdbc driver is in

C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC

i have placed my jar files in:
C:\apache-tomcat-5.5.23\webapps\ROOT\WEB-INF\lib

i have kept my source code in
C:\apache-tomcat-5.5.23\webapps\jbse\WEB-INF\classes\jbs

i am using the same ip tht i hav specified in the URL.and my database name is data1


Thanks & Regards,
Anubha.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
C:\apache-tomcat-5.5.23\webapps\ROOT\WEB-INF\lib

That's the directory where you need to put the jar files of the driver. The web application won't find them where you have them now.
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
i have placed my jar files in:
C:\apache-tomcat-5.5.23\webapps\ROOT\WEB-INF\lib

i have kept my source code in
C:\apache-tomcat-5.5.23\webapps\jbse\WEB-INF\classes\jbs



Your app is jbse and you are placing your driver's binary in ROOT/WEB-INF/lib?

you should have the driver's jar file in jbse/WEB-INF/lib..
anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
Hi,
Thanks a lot its working now.
but still i have got a query that why we had to make a lib folder in
WEB-INF and then keep sql server driver jar files in it.

Regards,
Anubha.
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
All the third party binaries that you need in the web application should be kept in WEB-INF/lib.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

OK, taking a step back:

A Servlet container, like Tomcat, hosts Servlets, but it is able to host multiple 'web applications' from a single Tomcat instance. We also call these 'application contexts', as each web application running in the container has its own area to run in. This is managed by the container to prevent applications sharing code or interfering with each other. There are ways to allow the web apps to communicate, but by default you don't want them too.

Each web application has their own directory in the webapps directory, so ROOT is one web application context and jbse is another, separate, web application context.

The configuration details for a web application is kept in its WEB-INF directory. This can be code such as classes in the WEB-INF/classes directory, jar files in the WEB-INF/lib directory, or things like config files and other resources protected from direct access form the outside world.

Therefore you want you application 'jbse' to access a Driver library, you need to put that library in the webapps/jbse/WEB-INF/lib directory to make it visible to the jbse application.
anubha goel
Greenhorn

Joined: Jul 06, 2007
Posts: 22
Thank you..
I got your point...

Regards,
Anubha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception: com.microsoft.jdbc