请教!!急!
操作系统是:XP;
怎样用JDBC连接SQL Server2000;
请说的详细点,初学者,谢谢!!

解决方案 »

  1.   

    import java.sql.*;
    public class Demo{
    Connection cnt = null;
    String st = "sun:jdbc:odbc:JdbcOdbcDriver";
    Class.forName(st);
    cnt = DriverManager.getConnection("jdbc:odbc:数据源名","数据库名","数据库密码");
    }在写这个连接之前先要配置好数据源
      

  2.   

    这个是用JDBC-ODBC桥连接的!?
      

  3.   

    import java.sql.*;
    public class c{
        private String gvDriver = ""; 
        private String gvUser = "sa"; 
        private String gvPassword = ""; 
        private Statement sta;
        private static String message = "连接成功";
        public Connection getConnection(){
            try{
                Class.forName("sun:jdbc:odbc:JdbcOdbcDriver");
                //gvDriver="jdbc:odbc:sqlserver://127.0.0.1:1433;DatabaseName=myMovie";
                gvDriver = "jdbc:odbc:myMovie";
                return DriverManager.getConnection(gvDriver, gvUser, gvPassword);
               }catch(Exception e){
                message = e.toString();
            }
            return null;
        }  
        public static void main(String args[]){
            c cc = new c();
            Connection con = cc.getConnection();
            System.out.println(message);
        }
    }
    还是出错java.lang.ClassNotFoundException:sun:jdbc:odbc:JdbcOdbcDriver
      

  4.   

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")和Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")有什么区别!?
      

  5.   

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
    这个是微软的jdbc驱动,官方的。Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
    这个是jdbc-odbc驱动,效率低下一般不要用。微软的jdbc驱动可以在官方网站上下载,用google收一下。
      

  6.   

    将安装驱动后产生的三个文件msbase.jar;mssqlserver.jar;msutil.jar
    放到C:\j2sdk1.4.2_05\jre\lib\ext目录下就可以编程了,不需要另外配置环境变量了!然后试着运行下面的代码!
     import java.sql.*;
     import java.io.*;
     import java.util.*;
     
     /**
     6.    This program tests that the database and the JDBC
     7.    driver are correctly configured.
     */
     class TestDB
     {
        public static void main (String args[])
        {
           try
           {
              runTest();
           }
           catch (SQLException ex)
           {
              while (ex != null)
              {
                 ex.printStackTrace();
                 ex = ex.getNextException();
             }
           }
           catch (IOException ex)
           {
              ex.printStackTrace();
           }
        }    /**
    32.       Runs a test by creating a table, adding a value, showing the table contents, and
    33.       removing the table.
    34.    */
        public static void runTest()
           throws SQLException, IOException
        {
           Connection conn = getConnection();
           try
           {
              Statement stat = conn.createStatement();          ResultSet result = stat.executeQuery("SELECT * FROM Mst_Company");
              while(result.next())
             {
              System.out.println(result.getString(1));
             }
           }
           finally
           {
              conn.close();
           }
        }    /**
           Gets a connection from the properties specified
           in the file database.properties
          @return the database connection
        */
        public static Connection getConnection()
           throws SQLException, IOException
        {
    try
    {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    }
    catch(Exception e)
    {
    System.out.println(e);
    }        String url ="jdbc:microsoft:sqlserver://192.168.1.24:1433;DatabaseName=marunaka";
           String username = "dbuser";
           String password = "qaz123";       return DriverManager.getConnection(url, username, password);
        }
     }修改一下,ip地址,数据库名,用户名,密码,sql语句,你就可以运行了!