Olá galera…
tenho que mudar uma determinada aplicação de máquina, mas estou com dúvida de como escrever o web.xml da aplicação para fazer um conexão com o banco de dados. Pode até ser que meu problema ser o servlet que uso para fazer a conexão com o banco. Meu web.xml atualmente está assim:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>invente</display-name>
<description>
Descritor do contexto de desenvolvimento.
</description>
<servlet>
<servlet-name>dev-invoker</servlet-name>
<servlet-class>org.apache.catalina.servlets.InvokerServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dev-invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
e o servlet que faz conexão com o banco:
Servlet.java
[code]
import java.io.;
import java.util.;
import java.sql.; // JDBC
// import java.security.cert.;
import javax.servlet.; // JSDK
import javax.servlet.http.;
import br.cefetce.lar.invente.EnredoBean;
public class RootSearchEnredo2 extends HttpServlet
{
Statement stmt; // objeto para execucao do comando SQL
PreparedStatement prepStmt;
ResultSet rs; // objeto para obtencao do resultado do comando SQL
ResultSet rsId;
Connection con;
// FormResultBean beanArray;
Vector vetor = new Vector();
static final String consultaBasica = "SELECT id, generaltitle, generalaggregationlevel, generalinstitution, generalresponsible, " +
"lifecyclecontributedate, technicalmediatype, educationallearningobjecttype from lominvente WHERE generallanguage LIKE ? "
+ "&& generalaggregationlevel LIKE ? && generalinstitution LIKE ? "
+ "&& technicalmediatype LIKE ? && technicaldeliverymode LIKE ? "
+ “&& educationallearningobjecttype LIKE ?”;
String consulta;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception exception)
{
}
con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/invente","root","rafael57");
//stmt = con.createStatement();
}
catch (SQLException exception)
{
System.out.println ("ERRO2 Servlet/JDBC> SQL Exception " + exception.getMessage());
}
} //init ===========================================================
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
log(req.getParameter(“showId”));
}
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
if (!(req.getParameter(“showId”)!=null) )
{
vetor.clear();
pesquisar(treat(req), req);
req.setAttribute(“vetor”,vetor);
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher("/jsp/enredo/ResultsSearch2.jsp");
rd.forward(req,res);
log(“dispachpu”);
}
else
{
req.setAttribute(“bean”,getById(req.getParameter(“showId”)));
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher("/jsp/enredo/ResultDetail.jsp");
rd.forward(req,res);
}
}//fim do doPost
private EnredoBean getById(String id)
{
String consulta = “SELECT * FROM lominvente WHERE id=” + id;
try
{
stmt = con.createStatement();
rsId = stmt.executeQuery(consulta);
rsId.next();
}
catch(SQLException e)
{
log("excecao no getbyid: " + e.getMessage());
}
return (makeBeanById(rsId));
}
private void pesquisar(ArrayList elem,HttpServletRequest req)
{
int param = 1;
try
{
consulta += “;”;
prepStmt = con.prepareStatement(consulta);
prepStmt.setString(param++,req.getParameter(“language”));
prepStmt.setString(param++,req.getParameter(“aggregation”));
prepStmt.setString(param++,req.getParameter(“institution”));
prepStmt.setString(param++,req.getParameter(“mediatype”));
prepStmt.setString(param++,req.getParameter(“deliverymode”));
prepStmt.setString(param++,req.getParameter(“learningobjecttype”));
for(int i=0;i<elem.size();i++)
{
String temp[] = (String []) elem.get(i);
for(int k=0;k<temp.length;k++)
{
prepStmt.setString(param++,temp[k]);
log(temp[k]);
}
}
rs = prepStmt.executeQuery();
for(int i=0;rs.next();i++)
{
vetor.addElement(makeBean(rs));
}
}//fim do try
catch(SQLException e)
{
System.err.println(e.getMessage());
}//fim do catch
finally
{
try
{
prepStmt.clearParameters();
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if ( rs != null )
{
rs.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}//fim do finally
}[/code]