This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception: com.microsoft.jdbc

 
anubha goel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two points were made.
Which didn't you understand; David's or Christophe's?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you put the Microsoft JDBC driver?
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the third party binaries that you need in the web application should be kept in WEB-INF/lib.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you..
I got your point...

Regards,
Anubha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic