java連線sql2005問題

trbing發表於2007-10-27
用jsp連線sql2005總是不成功!所以寫了個java程式碼測試一下,也不成功!

 import java.sql.*;
import javax.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class Test{
	public static void main(String args[]){
		String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	 	String URL1="jdbc:sqlserver://localhost:1433;DatabaseName=db_shopping";
	 	String USER="sa";
	 	String PASSWORD="";
	 	try{
	 		
	 	Class.forName(DRIVER).newInstance();
	 	 	
	 	Connection conn=DriverManager.getConnection(URL1,USER,PASSWORD);
	 	
	 	Statement stmt=conn.createStatement();
	 	
	 	ResultSet rs=stmt.executeQuery("select * from dbo.tab_sex");
	 	
	 	while(rs.next()){
	 		System.out.println(rs.getString("sex"));
	 		}}catch(Exception e){e.printStackTrace();}
	 }
 
 
	}

 
<p class="indent">

2005的驅動已經搞定!這個程式碼總是出錯說:com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. The
user is not associated with a trusted SQL Server connection.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(Unknown Source
)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Sour
ce)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at Test.main(Test.java:14)
高手幫幫忙啊!為什麼會Login failed for user 'sa'. ?謝謝!

相關文章