File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Connection Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection Problem" Watch "Connection Problem" New topic
Author

Connection Problem

michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Hello,
I'm trying to display a table from sql server with local connection(everything is in my computer). I'm using netbeans, apache tomcat as a service, sql server express 2008(using windows authentication) and I have the following code:


<%@ page language="java" import="java.sql.*"%>
<head>
<title> Northwind: JSP and SQLServer 2005 </title>
</head>
<body bgcolor>
<%
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (ClassNotFoundException e)
{
out.println("<h1>Driver not found:" + e + e.getMessage()+ "</h1>" );
}
try
{
Connection conn = DriverManager.getConnection("jdbc:sqlserver://MICHAEL-PC\\SQLEXPRESS:1433;database=Northwind;Integrated Security=True");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT * FROM Employees");
out.println( "<table>" );
while ( rs.next() )
{
String id = rs.getString("EmployeeID");
String ln = rs.getString("LastName");
String fn = rs.getString("FirstName");
String ti = rs.getString("Title");
out.println("<tr><td>"+id+"</td><td>"+ln+"</td><td>"+fn+"</td><td>"+ti+"</td>");
}
out.println( "</table>" );
conn.close();
} catch (Exception e)
{
out.println( "<h2>Exception: "+e.getMessage()+"</h2>" );
}
%>
</body>
</html>


After 3 or 4 exceptions I copied the sqljdbc4.jar in "C:\Users\Michael\Documents\NetBeansProjects\TestJDBC\build\web\WEB-INF\lib" folder,
I changed the TCP Port to 1433 (through configuration manager>Network configuration>Protocols for SQLExpress>TCP/IP>IPAll),
also tried to include to the library of the web project the sqljdbc4.jar but still no go.
I'm also using the odbc-jdbc bridge for the connection.

So, the latest error i'm getting when trying to run the project is: Exception: Login failed for user ''.
I've read solutions from others and the were saying to change the server authentication from windows authentication to sql server and windows authentication mode but it didn't work with me.

Please any suggestions would be more than helpful.
Thank you in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Login failed for user? But I don't see where you specify the user or the password. Perhaps you ought to do that.

And why on earth are you doing that code in a JSP? That's just about the worst possible choice of tool for debugging this sort of thing. Besides which you shouldn't have that code in a JSP anyway. Get it out of the view and put it in the model (a servlet or a supporting bean) where it belongs.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

When you run Tomcat as a service, it is the service user (by default: LocalSystem ) that runs your program, and that is authenticated.


Some ideas:
Run Tomcat service under a named user, and give proper rights to that user in SqlServer
Don't use Windows Authentication
Use Tomcat's built in mechanism to register data sources, in stead of setting the connection in your JSP.
Don't embed java code in your JSPs (not a good practice).

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Well that was the code for a short jsp tutorial for the databases course at my uni.
At the cslab we are using a username and a password and everything is ok, but at home with windows authentication things are getting unconfortable.
I'll give it a try with sql server authentication.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

michael delta wrote:Well that was the code for a short jsp tutorial for the databases course at my uni...
Can't you move to a different uni?
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Finally I've got a solution! I had to put the sqljdbc_auth.dll(downloaded it from msdn) in the system32 folder.
Thank you all!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Hmmm, if I'd seen this thread yesterday I would have told you you needed a DLL file for integrated authentication to work... Sorry for missing this thread...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Problem
 
Similar Threads
[ODBC SQL Server Driver][SQL Server]The multi-part identifier "voter.eno" could not be boound
basic SQL connection steps
No access to SQL Express 2005/2008 database
Instatiating java class inside a servlet
No suitable driver found for JDBC