estou tentando fazer uma conexão ms sql server no jsp e naõ estou conseguindo.o interessante é que se eu estiver usando j2se ele funciona blz, mais quando estou usando com j2ee(jsp) ele não pega. não consegue realizar a conexão.
ps: estou trabalhando em rede!
so que o detalhe é que se usar no netbeans ele pega perfeitamente bem!
alguem pode me ajudar?
vou postar aqui a classe
DBSqlServer.java
package database;
/**
* @author Felipe Marinho ®
* Classe para gerenciar toda a comunicação com o SQLServer
* Conecta; Desconecta; Executa comandos SQL
*/
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBSqlServer {
/* =============== CONFIGURA CONEXÃO DO BANCO DE DADOS =============== */
/*private String databaseURL = "jdbc:jtds:sqlserver://fachesf-009:1433/BDEmp_Fachesf";
private String user = "sa";
private String password = "pcsystem";
private String driverName = "net.sourceforge.jtds.jdbc.Driver";
static private Connection Conexao = null;*/
private String databaseURL = "jdbc:odbc:basedados1";
private String user = "sa";
private String password = "pcsystem";
private String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
static private Connection Conexao = null;
public DBSqlServer() throws Exception
{
if ( Conexao == null )
{
Class.forName( this.driverName );
System.out.println( "O Driver JDBC foi carregado com sucesso." );
}
}
/* ============== ESTABELECE CONEXÃO COM O BANCO DE DADOS ============== */
public void ConectarBanco( ) throws Exception
{
try
{
if ( Conexao == null )
{
System.out.print(this.databaseURL+" "+this.user+" "+this.password );
this.Conexao = DriverManager.getConnection( this.databaseURL, this.user, this.password );
System.out.println( "Conexão com o SqlServer efetuada com sucesso." );
}
}
catch ( SQLException Erro )
{
Erro.printStackTrace();
throw new Exception( "Não foi possível estabelecer conexão com o banco." );
}
}
/* ============== ENCERRA CONEXÃO COM O BANCO DE DADOS ============== */
public void EncerraConexao()
throws Exception
{
try
{
if ( Conexao != null )
{
Conexao.close();
Conexao = null;
System.out.println( "Banco de Dados encerrado com sucesso." );
}
}
catch( SQLException Erro )
{
Erro.printStackTrace();
throw new Exception( "Não foi possível desconectar do banco de dados." );
}
}
/* ============== EXECUTA OS COMANDOS INSERT, UPDATE, DETELE ============== */
public void ExecuteSQLUpdate( String ComandoSQLUpdate )
throws Exception
{
try
{
Statement Stmt = Conexao.createStatement();
Stmt.executeUpdate( ComandoSQLUpdate );
System.out.println( "Comando executado com sucesso." );
}
catch( SQLException Erro )
{
Erro.printStackTrace();
System.out.println( "Não foi possível executar o comando: " + ComandoSQLUpdate);
throw new Exception( "Não foi possível executar o comando de atualização." );
}
}
/* ======================= EXECUTA O COMANDO SELECT ======================= */
public ResultSet ExecuteSQLSelect( String ComandoSQLSelect )
throws Exception
{
try
{
Statement Stmt = Conexao.createStatement();
ResultSet rs = Stmt.executeQuery( ComandoSQLSelect );
System.out.println( "Comando executado com sucesso." );
return rs;
}
catch( SQLException Erro )
{
Erro.printStackTrace();
System.out.println( "Não foi possível executar o comando: " + ComandoSQLSelect);
throw new Exception( "Não foi possível executar a consulta desejada." );
}
}
}
index.jsp
<%@page import="database.*"%>
<%
try {
//Usuario user = new Usuario(1,35,1,"2981");
DBSqlServer db;
db = new DBSqlServer();
db.ConectarBanco();
}
catch(Exception e) {
out.println(e.getMessage());
}
%>
so retorna o erro da excessão. socorro…isso é pra ontem!