Ai vai os codigos
*************** e r r o ******************************
java.lang.NullPointerException
at db.dbConexao.fecharConexao(dbConexao.java:67)
at fase02.doPost(fase02.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service
********************* dbconexao ********************8
- Created on 06/10/2004
- To change the template for this generated file go to
- Window - Preferences - Java - Code Generation - Code and Comments
*/
/**
- @author JoelD
- To change the template for this generated type comment go to
- Window - Preferences - Java - Code Generation - Code and Comments
*/
package db;
import java.sql.*;
public class dbConexao {
private Connection connection;
private Statement statement;
public dbConexao()
{
try
{
// Connection connection = null;
Class.forName(“org.gjt.mm.mysql.Driver”);
connection = DriverManager.getConnection( “jdbc:mysql://localhost:3306/cafe?user=root”);
Statement statement = connection.createStatement();
}catch(ClassNotFoundException ex)
{
System.out.println(“Não foi possível encontrar o driver”);
} catch(SQLException ex)
{
System.out.println(“Não foi possível conectar ao servidor”);
}
// try
// {
// }catch(SQLException ex){
// System.out.println(“Não foi possível conectar ao servidor”);
// }
}
public synchronized void executeUpdate(String update)throws SQLException {
try{
statement.executeUpdate(update);
}catch(SQLException ex){
System.out.println("Não foi possível executar o update");
}
}
public synchronized ResultSet executeQuery(String query)throws SQLException {
try{
return statement.executeQuery(query);
}catch(SQLException ex){
System.out.println("Não foi possível executar a query");
throw ex;
}
}
public void fecharConexao(){
try{
statement.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
******************** fase02 ***************************
import java.io.;
import java.lang.;
import javax.servlet.;
import javax.servlet.http.;
import java.sql.*;
import db.dbConexao;
public class fase02 extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
res.setContentType( "text/plain" );
// String login = req.getParameter(“login”);
// String senha = req.getParameter(“senha”);
//montagem do comando
dbConexao con = null;
String sql="Select * from cliente where cdcliente = 25";
try {
con = new dbConexao();
ResultSet rs = con.executeQuery(sql);
PrintWriter out = res.getWriter();
//Pesquisa
if (rs.next()){
//criação da sessão
int cod = 1;
// HttpSession session = req.getSession(true);
// session.setAttribute(“autorizado”,new Integer(cod));
res.sendRedirect("/farmacia/admOpcoes.jsp");
}
else
res.sendRedirect("/farmacia/erroLogin.jsp");
} catch(SQLException e){
res.sendRedirect("/farmacia/errobd.jsp");
return;
} finally {
if (con!=null)
con.fecharConexao();
con=null;
}
}
}