package demo;
import com.microsoft.*;
import java.sql.*;
import java.net.*;public class Demo
{
        //驱动程序常量
        private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public static void main(String args[])
    {
        String url="jdbc:microsoft:sqlserver://HotJava:1433;User=sa;Password=123;DatabaseName=pubs";
        String qu="select * from titles";
        try {
                Class.forName(DRIVER_CLASS);
            }
            catch (ClassNotFoundException ex1)
            {
                ex1.printStackTrace();
            }        try {
            Connection con=DriverManager.getConnection(url);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(qu);
            while(rs.next())
            {
                System.out.println(rs.getString(1));
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException ex)
        {
                System.out.println(ex.getMessage());
                System.out.println(ex.getNextException());
        }
    }}
怎么我连接SQL2005,结果显示No suitable drivernull。。什么原因。。我导入包了的,哪有错吗

解决方案 »

  1.   

    你的JDBC驱动是SQLServer2005的吗? 还是2000的?
      

  2.   

    到微软网站去下载最新的sql2005驱动
      

  3.   

    java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
           Connection c = java.sql.DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;user=myuser;password=mypass;");
           System.out.println("Connected!");
    注意2005中URL的写法,驱动也要下新的