?
Solved

database connection from java

Posted on 2007-11-18
12
Medium Priority
?
335 Views
Last Modified: 2010-03-30
hello there,

i am trying to connect to a db from my java program.the database i log into does not have a username password.i mean i use windows authentication to login my db.but now when i try to connect from my java program i get this error

jdbc:sqlserver://localhost:1433; databaseName=ERP;user=""      ;password=""
Login failed for user '""'. The user is not associated with a trusted SQL Server connection
0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 16

Expert Comment

by:ellandrd
ID: 20306921
This example works perfect for me... give it a try.
import java.sql.*;
 
public class testConnection
{
    public static void main(String[] args) 
    {
        // sa is for system administrator account.  to log as your username replace sa with your username, but you must have access to the database with your username or it will not work...
DB db = new DB();
        db.dbConnect(
     "jdbc:jtds:sqlserver://localhost:1433/ERP","sa","");  
    }
}
 
class DB
{
    public DB() {}
 
    public voidn dbConnect(String db_connect_string, 
  String db_userid, String db_password)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
    db_connect_string, db_userid, db_password);
            System.out.println("connected");
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};

Open in new window

0
 

Author Comment

by:zolf
ID: 20306952

i access my db with ZAERI\zolf.but i get error in my java

jdbc:sqlserver://localhost:1433; databaseName=ERP;user=" ZAERI\zolf"      ;password=""
Login failed for user '" ZAERI\zolf"'. The user is not associated with a trusted SQL Server connection.
0
 

Author Comment

by:zolf
ID: 20307075

somebody please help
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:ellandrd
ID: 20307332
ok for starters check your connection string.

i see a white space in this line:  

user=" ZAERI\zolf"

>>The user is not associated with a trusted SQL Server connection.

this is your problem why it isnt working.  remove the whitespace and try again.  if it still wont work, then try another user.  if that dont work, then try the "sa" account as that should work if your MS SQL Server is configured correctly.

Finally if all that fails, then its not to do with the java application - its something wit the MS SQL Server setup so i would look into that...

ellandrd
0
 
LVL 92

Expert Comment

by:objects
ID: 20309341
you need to add login permissions on sql server.
You can do that via Enterprise Manager
0
 
LVL 92

Expert Comment

by:objects
ID: 20309348
> jdbc:sqlserver://localhost:1433; databaseName=ERP;user=" ZAERI\zolf"      ;password=""

is your login name just zolf, whats the ZAERI bit?
would also pass the user/pass as parameters, and not in the connection string
0
 

Author Comment

by:zolf
ID: 20310533

>then try the "sa" account

when installing sql server i chose windows authentication.so i did not create a passw for sa.
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 20310858
>>is your login name just zolf, whats the ZAERI bit?

objects, i think this could be their domain name. In my office when we connect to our MS SQL Server, we have to provide the domain name - DOMAIN\username

zolf did you remove the whitespace fdro the username parameter above and try that?

>>when installing sql server i chose windows authentication.so i did not create a passw for sa.

yes but this is for connecting with enterprize manager or other SQL tools like query analyser, not for a java application.  

the problem isnt with the java application as my example above worked fine for me - its to do with security on the SQL Server not allowing connections so you need to fix that first.   on our SQL Server, we created a user account for me and set some permissions to connect, read and write and after that my application connected fine.  i assume you have to do the same...

BTW did you try the "sa" account or did it not work either?
0
 

Author Comment

by:zolf
ID: 20310940

i can connect to the db if i use a sql login account with username,passwd. but not with login with windows authentication.

i think we cannot use windows authentication from java to connect to the db.
0
 
LVL 16

Accepted Solution

by:
ellandrd earned 1000 total points
ID: 20310961
see my last comment in your second thread regarding this issue - i provided example code that connected with windows authentication
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question