Solved

database connection from java

Posted on 2007-11-18
12
324 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 9

Expert Comment

by:ysnky
ID: 20307343
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 250 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printf performancy 11 99
Is Applet the way to go for my drag and drop system? 8 49
Coding for the first time 9 98
Selenium WebElement Login button findElement 9 11
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 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