Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
DriverManager.getConnection("jdbc:microsoft:sqlserver://ServerName:1433;databaseName=***";Username;Password");

解决方案 »

  1.   

    这些我都下了,安装时候没有被提示指定安装路径,也不知道装到什么地方去了,
    听说完全版的JDBC  for  MSSQL server的驱动是大概7M大小,你们这些都是2m大小的。
    你们看我这段程序:import java.sql.*;public class net {
        public static void main(String args[]) {
            String url = "jdbc:odbc:new";
            Connection con;
            String createString;
            createString = "create table myTABLE " +
                                "(COF_NAME VARCHAR(32), " +
                                "SUP_ID INTEGER, " +
                                "PRICE FLOAT, " +
                                "SALES INTEGER, " +
                                "TOTAL INTEGER)";
            Statement stmt;        try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch(java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }        try {
                con = DriverManager.getConnection(url, "sa", "");
                stmt = con.createStatement();
                stmt.executeUpdate(createString);
                stmt.close();
                con.close();        } catch(SQLException ex) {
                System.err.println("SQLException: " + ex.getMessage());
            }
        }
    }
    我是用JDBC-ODBC桥连接到一个SQL-SERVER,数据库名字是new
    我在上面建立了myTABLE的一个表。程序运行的很成功。
    但是我现在想换成直接用JDBC  for  MSSQL server的驱动连接
    于是我换了一些代码,新的如下:import java.sql.*;public class net2 {
        public static void main(String args[]) {
            String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=new";
            Connection con;
            String createString;
            createString = "create table myTABLE " +
                                "(COF_NAME VARCHAR(32), " +
                                "SUP_ID INTEGER, " +
                                "PRICE FLOAT, " +
                                "SALES INTEGER, " +
                                "TOTAL INTEGER)";
            Statement stmt;        try {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            } catch(java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }        try {
                con = DriverManager.getConnection(url, "sa", " ");
                stmt = con.createStatement();
                stmt.executeUpdate(createString);
                stmt.close();
                con.close();        } catch(SQLException ex) {
                System.err.println("SQLException: " + ex.getMessage());
            }
        }
    }
    但是这就死活地被扔出个了ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    我已经被这个程序折磨了3天了,
    我知道一定是我的JDBC驱动没安装成功,
    我要怎么做啊?
      

  2.   

    装完后看帮助文件要把驱动的三个jar文件放到classpath里去啊,要不就会找不到啊。
      

  3.   

    Connecting to a Database --------------------------------------------------------------------------------Once the driver is installed, you can connect from your application to your database in two ways: with a connection URL through the JDBC driver manager, or with a JNDI data source. This quick start explains how to establish your database connection using a connection URL. See "Using the SQL Server 2000 Driver for JDBC" for details on using data sources. You can connect through the JDBC driver manager with the method DriverManager.getConnection. This method uses a string containing a URL. Use the following steps to load the driver from your JDBC application. 1. Setting the Classpath 
    The SQL Server 2000 Driver for JDBC needs to be defined in your CLASSPATH variable. The CLASSPATH is the search string that your Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not on your CLASSPATH, you receive the error "class not found" when trying to load the driver. Set your system CLASSPATH to include the following entries, where install_dir is the path to your SQL Server 2000 Driver for JDBC installation directory: install_dir/lib/msbase.jar  
    install_dir/lib/msutil.jar  
    install_dir/lib/mssqlserver.jar Windows Example 
    CLASSPATH=.;c:\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;c:\Microsoft SQL Server 2000 Driver for JDBC 
    \lib\mssqlserver.jar UNIX Example 
    CLASSPATH=.;/home/user1/mssqlserver2000jdbc/lib/msbase.jar;/home/user1/mssqlserver2000jdbc/lib/msutil.jar;/home/user1/mssqlserver2000jdbc/lib/mssqlserver.jar 2. Registering the Driver 
    Registering the driver tells the JDBC driver manager which driver to load. When loading a driver using class.forName(), you must specify the name of the driver: com.microsoft.jdbc.sqlserver.SQLServerDriver For example: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  3. Passing the Connection URL 
    After registering the driver, you must pass your database connection information in the form of a connection URL. The following is a template URL for the SQL Server 2000 Driver for JDBC. Substitute the values specific to your database. jdbc:microsoft:sqlserver://server_name:1433  For example, to specify a connection URL that includes the user ID "username" and the password "secret": Connection conn = DriverManager.getConnection 
      ("jdbc:microsoft:sqlserver://server1:1433","username","secret");  NOTE: The server_name is an IP address or a host name, assuming that your network resolves host names to IP addresses. You can test this by using the ping command to access the host name and verifying that you receive a reply with the correct IP address. The numeric value after the server name is the port number on which the database is listening. The values listed here are sample defaults. You should determine the port number that your database is using and substitute that value. You can find the complete list of Connection URL parameters in "Connection String Properties" of this book.