Solved

database connection from java

Posted on 2007-11-18
12
311 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
count11 challenge 6 47
Free Alternative to JIRA 4 55
maven example error 3 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

707 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

17 Experts available now in Live!

Get 1:1 Help Now