Forums Register Login

Exception: com.microsoft.jdbc

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi,
i didn't understand your point as i am a newbie to java.
please tell me in detail.

Thanks & Regards,
Anubha.
+Pie Number of slices to send: Send
Two points were made.
Which didn't you understand; David's or Christophe's?
+Pie Number of slices to send: Send
Where did you put the Microsoft JDBC driver?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
All the third party binaries that you need in the web application should be kept in WEB-INF/lib.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you..
I got your point...

Regards,
Anubha.
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1488 times.
Similar Threads
HTTP method POST is not supported by this URL
servlets using JDBC
Running a simple java program
Servlet connection with Jdbc
servlets using JDBC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:25:57.