Solved

database connection from java

Posted on 2007-11-18
12
322 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 106
web application structure 18 102
Unhandled exception type Exception 18 43
jboss wildfly 10.1 10 241
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

830 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