iLeichun

当前位置: 首页 > Java

MyEclipse6.0连接SQL2000和2005

分类:Java   来源:网络   时间:2010-08-18 11:28:42

步骤:
1. 下载Microsoft SQL Server 2000 Driver for JDBC这个,并在MyEclipse 6.0中添加了引用
2. 加环境变量
    CLASSPATH值%JAVA_HOME%in;
    JAVA_HOME值C:Program FilesJavajdk1.6.0_02
    修改增加path值%JAVA_HOME%in;
3. 安装SQL 2000的SP3补丁
4. 在命令下测试telnet localhost 1433正常
代码如:
import java.sql.*;
public class HelloWorld {
   
    private static String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

     private static String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";

     private static Connection dbConn = null;
       
         public static void main(String [] args)
            {
             try
              {

               Class.forName(driverName);
               dbConn = DriverManager.getConnection(dbURL,"sa","");
               Statement sql = dbConn.createStatement();
               ResultSet set = sql.executeQuery("select * from jobs");
               while(set.next())
               {
                System.out.println(set.getString("job_id")+" "+
                  set.getString("job_desc"));
               }
               System.out.println("连接成功!");
               dbConn.close();
              } catch (ClassNotFoundException e)
              {
               e.printStackTrace();
              } catch (SQLException e)
              {
               e.printStackTrace();
              }
            }
}

测试成功!
连2005
1. 下载sqljdbc_1.1.1501.101_chs.exe并引入
2. 更改端口为5125(用360查出的)
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();
          }
    }

}

更多