连接数据库代码....
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
try
{
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SBookMC","","");
Statement sql = con.createStatement();
ResultSet rs = sql.executeQuery("select*from ManagersTable");
while(rs.next())
{String name = rs.getString("LoginName");
String password = rs.getString("PassWord");
String number = rs.getString("ManagerNum“)
jTextArea.setText(name);
jTextArea.setText(password);
jTextArea.setText(number);
}
con.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
eclipse控制台的报错信息:com.microsoft.sqlserver.jdbc.SQLServerException: 用户 '' 登录失败。该用户与可信 SQL Server 连接无关联。 atcom.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.processLogon(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dbtry.DBpanel.run(DBpanel.java:97)
at java.lang.Thread.run(Unknown Source)
虽然报的是 用户无效...但是一直怀疑是TCP的端口问题,或者是驱动包的问题
我在DOS下面使用 netstat -an 查询的协议端口中没有 TCP的1433端口.... 而且SQLserver2005运行的进程PCB中的PID号所对应的tcp端口是1434...请高人指点 我这个小白一下.....谢谢了
------解决方案--------------------
可以把程序中的端口改成1434试一下啊!
------解决方案--------------------
- Java code
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBAccess { private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static String url="jdbc:sqlserver://localhost:1433;databasename=SBookMC"; private static String user="sa"; private static String pwd="sa"; public static Connection getConnection(){ try { Class.forName(driver); Connection con=DriverManager.getConnection(url, user, pwd); System.out.println("con success"); return con; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void closeConnection(Connection con){ if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void closeStatement(Statement stm){ if(stm!=null){ try { stm.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void closeResultSet(ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { getConnection(); }}