java. sql. Sqlexception: user ‘admin’ login failed
•
Java
Disclaimer: I have never used SQL server before
I'm trying to connect to SQL Server Express using java code
public class Test1 { public static void main(String[] args) throws sqlException,ClassNotFoundException { Class.forName("net.sourceforge.jtds.jdbc.Driver"); String url = "jdbc:jtds:sqlserver://localhost:1433/POC;instance=MOHITCH-LAPTOP/sqlEXPRESS"; String user = "admin"; String password = "admin"; Connection conn = DriverManager.getConnection(url,user,password); Statement st = conn.createStatement (); ResultSet rs = st.executeQuery("select * from POC.dbo.poc_table"); while (rs.next()) { System.out.println(rs.getInt(1) + " " + rs.getString(2)); } } }
I got the exception:
Exception in thread "main" java.sql.sqlException: Login Failed for user 'admin'. at net.sourceforge.jtds.jdbc.sqlDiagnostic.addDiagnostic(sqlDiagnostic.java:372) at net.sourceforge.jtds.jdbc.Tdscore.tdsErrorToken(Tdscore.java:2820) at net.sourceforge.jtds.jdbc.Tdscore.nextToken(Tdscore.java:2258) at net.sourceforge.jtds.jdbc.Tdscore.login(Tdscore.java:603) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:352) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185) at java.sql.DriverManager.getConnection(UnkNown Source) at java.sql.DriverManager.getConnection(UnkNown Source) at my.java.Test1.main(Test1.java:16)
I also try to log in using MS SQL Server Management Studio 2014 I succeeded in doing it
This is my database structure:
Thank you very much for any help!! thank you
Solution
I think you need to modify some configurations in the server
Please follow the steps below. I hope it will help you
1. Open your sql Server Management Studio. 2. Database server right click and go to properties. 3. Choose Security option and check sql Server and Windows authentication mode. 4. Enable TCP/IP connection in sql Configuration Manager. 5. Restart your sql server service.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码