I want to connect my sql server database with java. here is the code I have
package myPackage;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://DESKTOP-6U7R1AC\\SQLEXPRESS;databaseName=sample1;";
String uname="DESKTOP-6U7R1AC\\Amit";
String pass="";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url,uname,pass);
System.out.println("command completed sucessfully");
Statement stmt =(Statement) conn.createStatement();
String query ="SELECT * FROM tblPerson";
ResultSet rs =((java.sql.Statement) stmt).executeQuery(query);
while(rs.next()){
System.out.println(rs.getInt(1) +" "+rs.getString(2) +" "+rs.getString(3) );
}
}
}
Please note that
- I am using Microsoft SQL server management studio 18.
- I have added the mssql-jdbc-8.4.1.jar in the build path.
my SQL server details
- server type: Database Engine
- server name: DESKTOP-6U7R1AC\SQLEXPRESS
- Authentication: Windows Authentication
- User name: DESKTOP-6U7R1AC\Amit
- Password:
I am getting the following error-
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'DESKTOP-6U7R1AC\Amit'. ClientConnectionId:497452a3-a4f3-40a8-b68e-864e269ad66a at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:283) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:129) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:37) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:5233) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3988) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:85) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3932) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7375) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3206) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2713) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2362) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at myPackage.Test1.main(Test1.java:22)
integratedSecurity=true
in the connection string, and make sure that the driver is setup correctly. See docs.microsoft.com/en-us/sql/connect/jdbc/… – Mark Rotteveel