0

i want to connect java to sql server. this is my code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication20;

/**
 *
 * @author HaMeD
 */
import java.sql.*;
import javax.swing.JOptionPane;

public class JavaApplication20 {

   public static void main(String[] args) 
    {
        DB db = new DB();
        db.dbConnect( "jdbc:jtds:sqlserver://HaMeD-PC:1433/Db_Test"," " ," ");
    }
}

class DB
{
    public DB() {}

    public void dbConnect(String db_connect_string, 
  String db_userid, String db_password)
    {
        try
        {
           // Class.forName("net.sourceforge.jtds.jdbc.Driver");
            String connectionUrl = "jdbc:sqlserver://HaMeD-PC:1433;DatabaseName=Db_Test;user=;Password=";
            Connection con = DriverManager.getConnection(connectionUrl);
           // Connection conn = DriverManager.getConnection(
   // db_connect_string, db_userid, db_password);
            System.out.println("connected");

        }
        catch (Exception e)
        {
           // JOptionPane.showMessageDialog(null, e.getMessage());
           e.printStackTrace();
        }
    }
//<editor-fold defaultstate="collapsed" desc="comment">




};
//</editor-fold>

and i dont have password for login in sqlserver. my error is:

 Login failed for user ''. ClientConnectionId:afdb6551-8788-4d80-9d93-e382aa5f77d7

please help me.

0

2 Answers 2

1

You are not joining SQL Server to Java .... You are connecting.

Secondly, the error message was from the sql server. This means the connection request was sent but the sql server refused to connect to that username/password. You can use windows authentication to connect in local machine. See documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

((You can use windows authentication to connect in local machine.))but how use it?
0

you pass the "db_connect_string" String to "dbConnect" method, but you are not using it to connect to the db (perhaps you copy&paste your "connectionUrl" variable and didn´t adapt)

1 Comment

Yes, I was about to comment on that. I did a little looking, it looks like thats whats happening. He copy and pasted, but didn't tweak it correctly. From the connectinUrl, it doesn't look like the right parameters. I am pretty sure you need to take off "DatabaseName", "user", and "password" (and also actually put an actual username).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.