回 帖 发 新 帖 刷新版面

主题:跪求,java 连 sql server 的问题


package jdbcproject;

import java.sql.*;

class Authors{
public static void main(String args[]){
    //Statement st;
    Connection conn;
    try{
      String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

      Class.forName(sDBDriver).newInstance();
      String sConnStr =
          "jdbc:microsoft:sqlserver://localhoat:8080;DatabaseName=pubs";
      String username = "sa";
      String password = "";
      conn = DriverManager.getConnection(sConnStr, username,
          password);
      Statement stmt = conn.createStatement();
      String query="select city from authors";
      ResultSet rs=stmt.executeQuery("query");
      while(rs.next()){
        String city=rs.getString("city");
        System.out.println(city);
      }

      stmt.close();
      conn.close();
    }

  catch(Exception em){
    System.out.println("出现的异常为: " +em);
  }

}
}


出现的异常为: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

回复列表 (共2个回复)

沙发

你可以试试下面的代码:
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con=DriverManager.getConnection("jdbc:odbc:serverdsn");
      //先配置ODBC数据源,serverdsn是数据源名,
      Statement stm=con.createStatement();
      ResultSet rs=stm.executeQuery("select job_id,job_desc from jobs");
      if(rs.next())
      {
           System.out.println(rs.getInt("job_id"));
           System.out.println(rs.getString("job_desc"));
       }

板凳

tomcat没有加载sql第三方的驱动,你把sql的第三方驱动,放到tomcat安装目录commn的lib文件夹内,就可以了

我来回复

您尚未登录,请登录后再回复。点此登录或注册