This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases 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 and Relational Databases
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
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
What does "is not working" mean, exactly?
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
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
"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: 20078
    
  30

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 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Divya Chandel
Ranch Hand

Joined: Jun 09, 2011
Posts: 47


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


Divya
SCJP1.6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jdbc connectivity of sql server
 
It's not a secret anymore!