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.
Its displaying "e" only(due to my weak coding).
I dont how to see exception detail.
johnny clarke
Greenhorn
Joined: Feb 02, 2013
Posts: 7
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35438
9
posted
0
"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
Joined: Feb 02, 2013
Posts: 7
posted
0
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
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.