步骤:
1. 下载Microsoft SQL Server 2000 Driver for JDBC这个,并在MyEclipse 6.0中添加了引用
2. 加环境变量
CLASSPATH值%JAVA_HOME%/bin;
JAVA_HOME值C:/Program Files/Java/jdk1.6.0_02
修改增加path值%JAVA_HOME%/bin;
3. 安装SQL 2000的SP3补丁
4. 在命令下测试telnet localhost 1433正常
代码如:
import
java.sql.
*
;
public class DbConnect ... {
private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dbURL = "jdbc:sqlserver://localhost:5125; DatabaseName=master";
private static Connection dbConn = null;
public static void main(String[] args) ...{
try
...{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL,"sa","000000");
Statement sql = dbConn.createStatement();
ResultSet set = sql.executeQuery("SELECT * FROM spt_values");
while(set.next())
...{
//System.out.println(set.getString("name")+" "+
//set.getString("number"));
System.out.println(set.getString(1));
}
System.out.println("连接成功!");
dbConn.close();
} catch (ClassNotFoundException e)
...{
e.printStackTrace();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
}
public class DbConnect ... {
private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dbURL = "jdbc:sqlserver://localhost:5125; DatabaseName=master";
private static Connection dbConn = null;
public static void main(String[] args) ...{
try
...{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL,"sa","000000");
Statement sql = dbConn.createStatement();
ResultSet set = sql.executeQuery("SELECT * FROM spt_values");
while(set.next())
...{
//System.out.println(set.getString("name")+" "+
//set.getString("number"));
System.out.println(set.getString(1));
}
System.out.println("连接成功!");
dbConn.close();
} catch (ClassNotFoundException e)
...{
e.printStackTrace();
} catch (SQLException e)
...{
e.printStackTrace();
}
}
}