源代码:
import java.sql.*;
public class MSSQL
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=niitDB";
Connection con = DriverManager.getConnection(url, "Administrator", "");
Statement state = con.createStatement();
ResultSet rs = state.executeQuery("select * from student");
//获得结果集的'元数据'
ResultSetMetaData md = rs.getMetaData();
//获得列数
int columnCount = md.getColumnCount();
while(rs.next())
{
for(int i=1; i <=columnCount; i++)
{
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
出现这样的异常:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection. <init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at chap1.MSSQL.main(MSSQL.java:14)
三个JAR包都导了
用JDBCODBC桥没有出错
请大家帮忙找找原因
------解决方法--------------------------------------------------------
数据库服务没开
------解决方法--------------------------------------------------------
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=niitDB";
应该是这两句出问题了..你去网上查查.
------解决方法--------------------------------------------------------
sqlserver在有些win上是无法直连的...这是sqlserver自身的问题...
你去下个sql sp3或sql sp4的补丁...就可以解决...
------解决方法--------------------------------------------------------
SQL Server2000要打一下SP3以上的补丁,把1433端口放开就可以了。SP3、SP4都可以。
------解决方法--------------------------------------------------------
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
没有打开1433
打上SP4补丁就可以了
------解决方法--------------------------------------------------------
打上SP4补丁
------解决方法--------------------------------------------------------
打上SP3以上的补丁才行。
------解决方法--------------------------------------------------------
为什么没有关连接啊 !!!
------解决方法--------------------------------------------------------
打上相应版本的SP3补丁即可解决即可解决问题 开放你的1433端口