我从这段代码得到空指针异常。我在库中导入了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