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.
2 Answers 2
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.
1 Comment
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)