1

我从这段代码中得到空指针异常。我已经在库中导入了 jtds-1.3.0.jar。我还在 SQL Server 配置管理器中将配置设置为使用端口 1433。

代码

public class Conexao 
{

  private static String driver = "net.sourceforge.jtds.jdbc.Driver";//driver
  private static String banco = "gerenciaRH"; //the name
  private static String host = "localhost"; //wheres the database
  private static String str_conexao = "jdbc:jtds:sqlserver://" + host + ":1433/" + banco; //string for connection
  private static String usuario = "sa"; //User
  private static String senha = ""; //passw
  private static Connection conexao = null;
  private static Statement state;

  public Statement conectar()
  {
    try 
    {
      if (conexao == null || conexao.isClosed()) 
      {
        Class.forName(driver).newInstance();
        conexao = (Connection) DriverManager.getConnection(str_conexao, usuario, senha);
        state = (Statement) conexao.createStatement();

        return state;
      }
      else 
      {
        return state;
      }
    }
   catch (ClassNotFoundException e) 
   {
      System.out.println(e);
      return null;
   }
   catch (SQLException e) 
   {
      System.out.println(e);
      return null;
   }
   catch (Exception e)
   {
      System.out.println(state);
      System.out.println(e);
      return null;
   }
  }
   public static void fechaConexaoBD () 
  {
      try 
      {
        conexao.close();
      }
      catch (SQLException e) 
      {
        e.printStackTrace();
      }
  }
} //close conection

控制台

java.sql.SQLException:网络错误IOException:连接被拒绝:线程“AWT-EventQueue-0”中的连接异常java.lang.NullPointerException

4

0 回答 0