日期:2014-05-20  浏览次数:20825 次

求助 java连接sqlserver2005 不能连接
代码如下(上网找的)

package classsource;

import java.sql.*;

public class Database {
  public static Connection cn;
  public static Statement st;
  public static ResultSet rs;

  public static boolean joinDB() {
  boolean joinFlag;
  try {
  joinFlag = true;
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  cn = DriverManager.getConnection("jdbc:odbc:EmployeeInformationMS","sa","");
   
  cn.setCatalog("EmployeeInformationMS");
  System.out.println("数据库连接成功");
  st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_READ_ONLY);
  return joinFlag;

  } catch (SQLException sqlEx) {
  System.out.println(sqlEx.getMessage());

  joinFlag = false;
  return joinFlag;

  } catch (ClassNotFoundException notfoundEX) {
  System.out.println(notfoundEX.getMessage());

  joinFlag = false;
  return joinFlag;
  }
  }

  public static boolean executeSQL(String sqlString) {
  boolean executeFlag;
  try {
  st.execute(sqlString);
  executeFlag = true;
  } catch (Exception e) {
  executeFlag = false;
  System.out.println("sql exception:" + e.getMessage());
  }
  return executeFlag;
  }


  public static boolean query(String sqlString) {

  try {
  rs = null;
  //System.out.println(sqlString);
  rs = st.executeQuery(sqlString);
  } catch (Exception Ex) {
  System.out.println("sql exception:" + Ex);
  return false;
  }
  return true;
  }
}

不知道哪里出现问题了
出现sql exception:java.lang.NullPointerException 代表什么意思呢?
由于是菜鸟 并没有学习到JDBC连接数据库 但由于作业要求...相当匆忙的了解了一下 由于java知识不扎实导致有点知识断层的感觉 想请问各位 如果要解决如上我要连接数据库的问题 该如何解决呢?

------解决方案--------------------
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection("jdbc:odbc:EmployeeInformationMS","sa","");
虽然没用过sqlserver,可是
这个sun.jdbc.odbc.JdbcOdbcDriver是sql2005驱动类的名称,
jdbc:odbc:EmployeeInformationMS这个URL都米有端口