Connecting SQL Server 2008 to Java: Login failed for user error?

Would this other question possibly be the same issue: MS SQL Server 2005 Express x86 - its port is unreachable - help .

Up vote 0 down vote favorite share g+ share fb share tw.

I am trying to connect my Java code to a Microsoft SQL Server 2008 R2 Express database. I have downloaded the Microsoft SQL Server JDBC Driver 3.0 and added the sqljdbc4. Jar to my classpath.

I am using Netbeans and have included the sqljdbc4. Jar in my project also. I created a database in the SQL Server Management Studio called TestDB1 and added some columns and values that I will use for testing.

I changed from Windows Authentication Mode by right clicking on the server JACOB=PC\SQLEXPRESS->Properties->Secuity and changing from Windows Authentication Mode to SQL Server and Windows Authentication Mode. I then created a new login by right clicking on the Login folder in the window explorer under JACOB-PC/SQLEXPRESS->Secuity Folder->Logins Folder and added a new login. I gave it the name jhaip2, switched to SQL Server authentication and the set the password to jacob.

Enforce password policy and enforce password expiration are unchecked. The default database is set to TestDB1. Then under TestDB1->Secuity->Users->jhaip2->Database role membership I set jhaip2 to db_owner (I couldn't log in to the database in the management studio without doing this, probably not the right thing to do?

). I then restarted the server. Now for my java code, it is basically a direct copy of the JDBC Driver 3.0 Sample code except without windows authentication.

Package databasetest1; import java.sql. *; public class connectURL { public static void main(String args) { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=TestDB1;"; // Declare the JDBC objects.

Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Establish the connection. Class. ForName("com.microsoft.sqlserver.jdbc.

SQLServerDriver"); System.out. Println("Driver okay"); con = DriverManager. GetConnection(connectionUrl,"jhaip2","jacob"); System.out.

Println("Connection Made"); } // Handle any errors that may have occurred. Catch (Exception e) { e.printStackTrace(); } finally { if (rs! = null) try { rs.close(); } catch(Exception e) {} if (stmt!

= null) try { stmt.close(); } catch(Exception e) {} if (con! = null) try { con.close(); } catch(Exception e) {} } } } When I run, it prints out "Driver okay" so I am assuming my driver is set up correctly. Then it prints the error: com.microsoft.sqlserver.jdbc.

SQLServerException: Login failed for user 'jhaip2'. It does not matter what username I use, it always fails. I have a feeling I am setting up the user wrong.

If anyone could give me some help on how to properly set up a user or any guidance in how to simply connect to a SQL Server database in Java, I would appreciate it. Java sql sql-server sql-server-2008 jdbc link|improve this question asked Jun 19 '11 at 16:30jhaip5716 80% accept rate.

Perhaps your sqlserver is configured to use the windows authenification instead of the sql server authentification. – Omnaest Jun 19 '11 at 16:38 @user625146 I forgot about that, agreed with change Windows Autentifications to the Mixed Mode, +1 and SQL2008R2 has by defaul closed Networks Pipes too... – mKorbel Jun 19 '11 at 16:45 Would this help - thusithamabotuwana.wordpress.com/2012/01...? – user1167696 Jan 24 at 18:38.

Would this other question possibly be the same issue: MS SQL Server 2005 Express x86 - its port is unreachable - help. You have to make sure that SQL Server is listening on the correct port before you can connect via a TCP connection using JDBC.

The correct ports were open but the problem was in the authentication mode described in my answer. Thanks for your help though. – jhaip Jun 24 '11 at 1:17.

When I looked in the SQL Server log files, it was saying that I couldn't log in because SQL Server was in Windows Authentication mode, even though in the program it was set to Mixed Authentication Mode. When installing, I had set it up in Windows Authentication mode but changing the settings in the program would not change it from Windows Authentication mode. I removed SQL Server 2008 and all related programs from my computer and installed a fresh copy, this time with SQL Server Authentication.

Everything works correctly now. I don't know why SQL Server had a problem with changing the authentication mode, but it works now so I am happy.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions