问题已更新(不再手动加载DriverManager,而是使用getConnection()方法):
package guii;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcSQLServerConnection {
public static void main(String[] args) {
Connection conn = null;
Statement st=null;
ResultSet rs=null;
try {
String dbURL = "jdbc:sqlserver://ASUS\\YES:1433;integratedSecurity=false";
String user = "sa";
String pass = "12345";
conn = DriverManager.getConnection(dbURL,user,pass);
String query="SELECT * FROM test";
st=conn.createStatement();
rs=st.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
问题是此代码的结果异常。我无法知道抛出特定异常的原因。
用户名,密码和服务器名称经过双重检查,确切无误。
目前抛出此异常:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:21504e75-b630-4f71-b76b-88b25836969c
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:251)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:81)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:3077)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2360)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1404)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at guii.JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:23)
我可以使用integratedSecurity = true运行程序,但我无法从数据库中选择表。我甚至不知道是什么问题,但我已经在SQL Server Management Studio中尝试过,并且它已成功登录。我真的放弃了。首先是将它与我的用户和密码连接起来。其次是如何从我的数据库中选择表。请帮我。谢谢。