Two Laptop Bag*
The moose likes JDBC and the fly likes jdbc connectivity of sql server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc connectivity of sql server" Watch "jdbc connectivity of sql server" New topic
Author

jdbc connectivity of sql server

Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

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
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
What does "is not working" mean, exactly?


Ping & DNS - my free Android networking tools app
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Mean connection is not establishing.
Its throwing exception.
johnny clarke
Greenhorn

Joined: Feb 02, 2013
Posts: 7
Singh Anisha wrote:Mean connection is not establishing.
Its throwing exception.

Can we see the exception?
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

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
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: 41600
    
  55
"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
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

Joined: May 09, 2012
Posts: 100

Its showing "Getting connection problem"
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

what could be possible reasons.... driver is not loading
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Divya Chandel
Ranch Hand

Joined: Jun 09, 2011
Posts: 46


In catch block can you put e.printStackTrace(),so it will give you error details.


Divya
SCJP1.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbc connectivity of sql server