Olá pessoal tudo bom?
Sou meio iniciante em java e não sei se este é tópico certo para esta mensagem mas, vamos lá:
Eu estou tentando conectar um servlet no apache tomcat 5.5.9 ( eu utilizao Windows XP com Service Pack 2 ) com meu banco de dados Firebird porém, não consigo. Eu fiz uma aplicação em java normal na qual eu adiconei o arquivo firebirdsql-full.jar ao classpath e consegui conectar sem problemas. Porém, tentei adicionar o caminho deste arquivo jar ao classpath do tomcat no windows e, mesmo assim, não consigo fazer o servlet rodar ele sempre dá o erro de An error ocurred. Please try again later do código abaixo e, também, eu não consigo fazer ele exibir o erro exato que impede a inserção dos dados na tabela guestbook ( Seu alguém puder me indicar como fazer isso já é um começo ) Eu tenho uma base de dados( books.gdb e uma tabela guestbook criada no banco de dados ). Abaixo está o código:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class GuestBookServlet extends HttpServlet {
private Statement statement = null;
private Connection connection = null;
private String URL = "jdbc:firebirdsql:127.0.0.1/3050:c:\\firebird\\guestbook.gdb";
private String usr = "sysdba";
private String psw = "master";
String conexao;
String Problem;
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
try{
Class.forName( "org.firebirdsql.jdbc.FBDriver" );
connection =
DriverManager.getConnection( URL,usr,psw );
conexao = "conectado ao banco com sucesso.";
}
catch ( Exception e ){
e.printStackTrace();
connection = null;
}
}
public void doPost( HttpServletRequest req,
HttpServletResponse res )
throws ServletException, IOException
{
String email, firstName, lastName, company, snailmailList,
cppList, javaList, vbList, iwwwList;
email = req.getParameter( "Email" );
firstName = req.getParameter( "FirstName" );
lastName = req.getParameter( "LastName" );
company = req.getParameter( "Company" );
snailmailList = req.getParameter( "mail" );
cppList = req.getParameter( "c_cpp" );
javaList = req.getParameter( "java" );
vbList = req.getParameter( "vb" );
iwwwList = req.getParameter( "iwww" );
String parametros = "parametros recebidos com sucesso!";
PrintWriter output = res.getWriter();
res.setContentType( "text/html" );
if ( email.equals( " " ) || firstName.equals( " " )||
lastName.equals( " " ) ){
output.println( "<H3> Please click the back " + "button and fill in all " +
"fields. </H3>" );
output.close();
return;
}
/* Nota: o banco de dados guestbook na verdade contém os campos
* Address1, Address2, City, State e Zip que não são
* usados neste exemplo. Entretando, a inserção no banco de
* dados, mesmo assim, precisa levar em consideração estes campos. */
boolean success = insertIntoDB(
"'" + email + "','" + firstName + "','" + lastName + "','" +
company + "','" +
( snailmailList != null ? "yes" : "no" ) + "','" +
( cppList != null ? "yes" : "no" ) + "','" +
( javaList != null ? "yes" : "no" ) + "," +
( vbList != null ? "yes" : "no" ) + "','" +
( iwwwList != null ? "yes" : "no" ) + "'" );
if( success )
output.println( "<H2> Thank You " + firstName +
" for registering. </H2>" );
else
output.print( "<H2> An error ocurred. " +
"Please try again later.</H2><BR>" + "<H3> Status da conexao: " +
conexao + "<BR>" +
email + "<BR>" +
firstName + "<BR>" +
lastName + "<BR>" +
company + "<BR>" +
( snailmailList != null ? "yes" : "no" ) + "<BR>"+
( cppList != null ? "yes" : "no" ) + "<BR>" +
( javaList != null ? "yes" : "no" ) + "<BR>" +
( vbList != null ? "yes" : "no" ) + "<BR>" +
( iwwwList != null ? "yes" : "no" ) + "<BR>" +
"Valor de problema: " + Problem +
"</H3>" );
output.close();
}
private boolean insertIntoDB( String stringtoinsert )
{
try{
statement = connection.createStatement();
statement.execute(
"INSERT INTO guestbook VALUES(" +
stringtoinsert + ");" );
statement.close();
}
catch ( Exception e ){
Problem = "ERROR: Problems with adding new entry";
e.printStackTrace();
return false;
}
return true;
}
public void destroy()
{
try {
connection.close();
}
catch ( Exception e ){
System.err.println( "Problem closing the database" );
}
}
}
Se alguém puder me ajudar eu agradeço.
Obrigado.
Rafael.