jdbc连接sqlserver问题,输入下面代码,编译能通过,运行时有异常。sa用户能登录成功!!
importjava.sql.*;publicclassaa{publicstaticvoidmain(Stringargs[])throwsException{Clas...
import java.sql.*;
public class aa{
public static void main(String args[]) throws Exception{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=yy","sa","123");
Statement stmt =conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Score s");
while(rs.next())
{
System.out.println(rs.getString(1)
+rs.getString(2)
+rs.getDate(3)
+rs.getInt(4)
+rs.getInt(5)); }
if(rs!=null)
{
rs.close(); }
if(stmt!=null)
{
stmt.close(); }
if(conn!=null)
{
conn.close(); }
}
}
异常代码
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 用户
'sa' 登录失败。该用户与可信 SQL Server 连接无关联。 ClientConnectionId:43b10738
-d170-48da-9102-ced8630372be
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254
)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerC
onnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConne
ction.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServer
Connection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecu
te(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLSe
rverConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at aa.main(aa.java:8) 展开
public class aa{
public static void main(String args[]) throws Exception{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=yy","sa","123");
Statement stmt =conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Score s");
while(rs.next())
{
System.out.println(rs.getString(1)
+rs.getString(2)
+rs.getDate(3)
+rs.getInt(4)
+rs.getInt(5)); }
if(rs!=null)
{
rs.close(); }
if(stmt!=null)
{
stmt.close(); }
if(conn!=null)
{
conn.close(); }
}
}
异常代码
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 用户
'sa' 登录失败。该用户与可信 SQL Server 连接无关联。 ClientConnectionId:43b10738
-d170-48da-9102-ced8630372be
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254
)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerC
onnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConne
ction.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServer
Connection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecu
te(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLSe
rverConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at aa.main(aa.java:8) 展开
1个回答
2012-06-20
展开全部
at aa.main(aa.java:8)这行有错 看下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询