• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Problem

 
michael delta
Ranch Hand
Posts: 35
Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
michael delta
Ranch Hand
Posts: 35
Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic