0

我正在尝试在远程桌面上连接 sql server 2008。我的远程桌面 IP 是192.168.175.174. 用户是User3,密码是user3,数据库是apache_camel

它给出了这样的错误任何伙伴都可以帮助我连接

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'User3'.
at     com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196    )
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.Sqlserver_connection.main(Sqlserver_connection.java:22)

这是我的课程代码:

package database;

import java.sql.*;

public class Sqlserver_connection {
    public static void main(String[] args) {

        // Create a variable for the connection string.
        String connectionUrl =        "jdbc:sqlserver://192.168.175.174:1433;DatabaseName=apache_camel;user=User3;password=user3";
        //String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=YourDBName;user=UserName;Password=YourPassword" 
        //"databaseName=HelloWorld;integratedSecurity=true;";

        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

            try {
                // Establish the connection.
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(connectionUrl);
                    System.out.println(con);
                    // Create and execute an SQL statement that returns some data.
                    String SQL = "SELECT TOP 1000 [username],[password] FROM [apache_camel].[dbo].[login]";
                    stmt = con.createStatement();
                    rs = stmt.executeQuery(SQL);

                    // Iterate through the data in the result set and display it.
                    while (rs.next()) {
                        System.out.println(rs.getString(1) + " " + rs.getString(1));
                    }
            }

        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            if (rs != null) try { rs.close(); } catch(Exception e) {}
                if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                if (con != null) try { con.close(); } catch(Exception e) {}
        }
    }
}
4

1 回答 1

0

好吧,不确定您是否“必须”使用 SQLServer 驱动程序,这是您可以用于连接的另一个驱动程序(它在纯 java 应用程序和 android 应用程序上对我有用),http://jtds.sourceforge.net/真的易于使用,您可以通过类 DriverManager 静态方法 getConnection(String url, String username, String password) 为用户提供凭据,例如:

连接连接 = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.3.67:1433/androiddb", "sa", "1234");

这是我几周前发表的一篇文章,我回答了自己,它专门展示了如何设置与 jtds 的连接,是的,它适用于 android,但你可以跳过特定的 android 步骤(它适用于 MS SQL 2008,我测试它在纯java程序上)。

JTDS for MS SQL in android with ADT

希望对您有所帮助,问候。

于 2013-05-27T15:30:52.097 回答