Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc connectivity of sql server

 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class LogInServlet extends HttpServlet {
String userName;
String password;
PrintWriter pw;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
pw = response.getWriter();
userName=request.getParameter("username");
password=request.getParameter("password");
openConnection();
}
public void openConnection(){
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String conURL="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
String userName="sa";
String password="password";
Connection con=null;
try{
Class.forName(driverName);
}
catch(Exception e){
pw.println("<html><center><body>"+ e +"</body></center></html>");
}
try{
con=DriverManager.getConnection(conURL,userName,password);
}
catch(Exception e){
pw.println("<html><center><body>"+ e +"</body></center></html>");
}

}

I am a newbie and trying this code to connect to sqlserver. i put sqljdbc4.jar to lib of WEB-INF and set its classpath.
But the connection is not working.
Please guide me regarding same.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "is not working" mean, exactly?
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mean connection is not establishing.
Its throwing exception.
 
johnny clarke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh Anisha wrote:Mean connection is not establishing.
Its throwing exception.

Can we see the exception?
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its displaying "e" only(due to my weak coding).
I dont how to see exception detail.
 
johnny clarke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh Anisha wrote:Its displaying "e" only(due to my weak coding).
I dont how to see exception detail.


Try something like this. You are just printing e and not what is in e.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"e.getMessage()" is what one would usually print in these circumstances. An additional "e.printStackTrace()" would be even more helpful. The output of that would go to the server's log files.
 
johnny clarke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:"e.getMessage()" is what one would usually print in these circumstances. An additional "e.printStackTrace()" would be even more helpful. The output of that would go to the server's log files.


I see. Im quite new myself to servlets and I'm using e.toString with log4j so I then tail the logs. Cheers
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its showing "Getting connection problem"
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what could be possible reasons.... driver is not loading
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the driver wouldn't load you would get a ClassNotFoundException, or perhaps a "no suitable driver" error message. If it can't connect then the problem is either in SQL Server or in the network in between.
 
Divya Chandel
Ranch Hand
Posts: 47
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In catch block can you put e.printStackTrace(),so it will give you error details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic