Olá,
Estou garimpando no portal já ha algum tempo, atrás de um exemplo que possa me clarear as idéias.
Estou executando alguns exemplos do livro de Deitel, Java Como Programar. Estou na sessão de Servlets Cap.19 Exercício. 19.16 e sequencias.
Já lí alguns tut sobre JDBC e acesso a banco, mas ainda nada.
O meu problema é que na hora do insert no banco alguma coisa dá errado e [color=“green”]é retornado um erro que está presente no código servlet[/color]:
[color=“red”]An error occurred. Please try again later.[/color]
Já vasculhei mas não acho um erro aparente.
Será erro de conexão no banco?
Já fiz as configurações do banco ACCESS no ODBC e dei o mesmo nome que é referenciado na URL no código: GuestBook.
Já fiz outros testes de servlet, todos realizados com sucesso, mas este de conexão a banco tá dando trabalho.
O código servlet é:
// Fig. 19.16: GuestBookServlet.java
// Three-Tier Example
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:odbc:GuestBook";
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connection =
DriverManager.getConnection( URL, "", "" );
}
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" );
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;
}
output.print( "<H2>Teste </H2>" );
/* Note: The GuestBook database actually contains fields
* Address1, Address2, City, State and Zip that are not
* used in this example. However, the insert into the
* database must still account for these fields. */
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" ) + "'" );
output.print( "<H2>Teste </H2>" );
if ( success )
output.print( "<H2>Thank you " + firstName +
" for registering.</H2>" );
else
output.print( "<H2>An error occurred. " +
"Please try again later.</H2>" );
output.close();
}
private boolean insertIntoDB( String stringtoinsert )
{
try {
statement = connection.createStatement();
statement.execute(
"INSERT INTO GuestBook values (" +
stringtoinsert + ");" );
statement.close();
}
catch ( Exception e ) {
System.err.println(
"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" );
}
}
}
Estou chamando este servlet do html abaixo:
<!-- Fig. 19.17: GuestBookForm.html -->
<HTML>
<HEAD>
<TITLE>Deitel Guest Book Form</TITLE>
</HEAD>
<BODY>
<H1>Guest Book</H1>
<FORM
ACTION=http://localhost:8080/NovoServlet/GuestBookServlet
METHOD=POST><PRE>
* Email address: <INPUT TYPE=text NAME=Email>
* First Name: <INPUT TYPE=text NAME=FirstName>
* Last name: <INPUT TYPE=text NAME=LastName>
Company: <INPUT TYPE=text NAME=Company>
* fields are required
</PRE>
<P>Select mailing lists from which you want
to receive information<BR>
<INPUT TYPE=CHECKBOX NAME=mail VALUE=mail>
Snail Mail<BR>
<INPUT TYPE=CHECKBOX NAME=c_cpp VALUE=c_cpp>
<I>C++ How to Program & C How to Program</I><BR>
<INPUT TYPE=CHECKBOX NAME=java VALUE=java>
<I>Java How to Program</I><BR>
<INPUT TYPE=CHECKBOX NAME=vb VALUE=vb>
<I>Visual Basic How to Program</I><BR>
<INPUT TYPE=CHECKBOX NAME=iwww VALUE=iwww>
<I>Internet and World Wide Web How to Program</I><BR>
</P>
<INPUT TYPE=SUBMIT Value="Submit">
</FORM>
</BODY>
</HTML>
Meu web.xml está assim:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>GuestBookServlet</servlet-name>
<servlet-class>GuestBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GuestBookServlet</servlet-name>
<url-pattern>/GuestBookServlet</url-pattern>
</servlet-mapping>
</web-app>
Os arquivos acima estão assim:
C:/
|—Tomcat 5.0
----|----webapps
---------|-----NovoServlet ------->GuestBookForm.html e -->GuestBook.mdb
-----------------|
-----------------|------WEB-INF------->web.xml
-------------------------|
-------------------------|--------classes ------>GuestBookServlet.class
Espero que possam me ajudar :?: :sad: