0

I am Using Windows Authentication (Please kept in mind).

As there is no user name and password.

then

why the following code gives me error?

public class Conection
{
public static void main(String a[]) throws ClassNotFoundException, SQLException
{
    try
    {
        String url = "jdbc:sqlserver://localhost\\MALIKUSMANNAWAZ:1433;databaseName=ali";   
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection(url);
        System.out.println("connection created");
        Statement st=conn.createStatement();
        String sql="select * from Login_System";
        ResultSet rs=st.executeQuery(sql);
        while(rs.next())
        {
            System.out.println("Name: "+rs.getString(1));
            //System.out.println("Address : "+rs.getString(2));
        }
        if(st!=null)
        st.close();
        if(conn!=null)
            conn.close();
    }
    catch(SQLException sqle)
    {
        System.out.println("Sql exception "+sqle);
    }
}
}

Please Kept in mind that my PC Name is:

MALIKUSMANNAWAZ

using:

Windows Authentication

Database Name:

ali

IDE:

SQL Server 2012
Usman Nawaz
  • 82
  • 1
  • 4
  • 21

2 Answers2

2

add ;integratedSecurity=true to your connection string. While you are not required to submit credentials if you are using windows authentication you still have to tell your connection to use your windows logon.

here is microsoft's article on jdbc connection strings. https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

Matt
  • 13,833
  • 2
  • 16
  • 28
  • Now the error is: Jun 27, 2016 12:35:47 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path – Usman Nawaz Jun 26 '16 at 19:36
  • http://stackoverflow.com/questions/17277001/dll-missing-in-jdbc is an exact duplicate of ;integrated security failed to load.... That answer says you need to change the path of sqlddbc.jar and then links to the accepted answer on this post of changing the path. http://stackoverflow.com/questions/957700/how-to-set-the-java-library-path-from-eclipse/958074#958074 – Matt Jun 26 '16 at 19:39
  • Now Done it, as the answer is given below. – Usman Nawaz Jun 28 '16 at 00:46
0

Completed As Given:

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\MALIKUSMANNAWAZ:1433;databaseName=ali","sa","dbase");

        System.out.println("connection created");

        Statement st=conn.createStatement();

        String sql="select * from Login_System";

        ResultSet rs=st.executeQuery(sql);
Usman Nawaz
  • 82
  • 1
  • 4
  • 21