需要SQLSERVER的JDBC驱程,可以到MS的网址里下载~
具体的代码与普通连接数据库没有什么区别。

解决方案 »

  1.   

    一个简单的例子:package jmodal;import java.sql.*;
    import java.io.*;
    import java.util.*;public class SQLDemo{   public static void main(String args[]) throws Exception{
           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
           Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CreatWall;user=sa;Password=");
           Statement st=conn.createStatement();
           String s="select * from c_card";
           ResultSet rs=st.executeQuery(s);
           while (rs.next())
           {
             System.out.println(rs.getString(1)+"<br>");
             System.out.println(rs.getString(2)+"<br>");
           }
       }
    }
      

  2.   

    1.下载JDBC FOR SQLSERVER 的驱动:
    http://www.microsoft.com/downloads/details.aspx?FamilyID=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&DisplayLang=en#filelist
    点击setup.exe下载驱动
    2.下载后开始安装,就用默认设置安装,会被安装到:
       C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC
    3.把C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib
      下面的mssqlserver.jar,msbase.jar,msutil.jar三个文件拷贝到你的
       JDK主目录\jre\lib\ext下面,现在JDBC驱动就算配置好了
    4.编译运行测试程序:
      可以使用楼上提供的程序,不过要做些改动: 
     Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;
    DatabaseName=CreatWall;user=sa;Password="");
    把上面的CreatWall改为你自己的数据库名,如果没有可以用系统里的pubs库
    如果sa设置了密码,把Password=""换成你的密码,如果没密码就不用改
    String s="select * from c_card";
    把c_card换为刚才你设置连接数据库里的某个表名
      

  3.   

    我给的JDBC驱动下载链接如果有问题,请用:
    http://www.microsoft.com/china/sql/downloads/jdbc_sp1.asp
      

  4.   

    下面也是个简单的例子:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.sql.SQLException;
    public class sql1{
    void display(){
    Connection con=null;
        String username="sa";
        String password="";
        String url="jdbc:odbc:ygq1";//数据源名为ygq
           try{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         con=DriverManager.getConnection(url,username,password);
         Statement stm=con.createStatement();
        stm.executeUpdate("create table ygq1(number int,name char(10),phone char(20))");//如果你有表了,这儿可以写其它的SQL语句,不过要把结果显示出来,还要用到楼上的一些代码,这儿就不详述了,看看就知道

    }catch(ClassNotFoundException e){System.out.println(e);}
    catch(SQLException e){System.out.println(e);}
    finally{
    try{
    if(con!=null)
    con.close();
    }
    catch(SQLException e){System.out.println(e);}
    }
    }public static void main(String args[]){
    sql1 app=new sql1();
    app.display();
     }
    }
    //连接数据库都是统一模式
      

  5.   

    不过有一点要小心,如果是windows2000,好像有个sql server2000的jdbc需要将win2000升级到sp3不然不能用