Solved

database connection from java

Posted on 2007-11-18
12
316 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 53
session migration servlets 2 24
even odd program using while loop 3 25
servlet example issue 6 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now