Como Usar servlet e mysql para inserir dados?

Estou tantando desenvolver um página simples de cadastro mais não estou conseguindo inserir os dados no banco.

abaixo tenho ao codigo do arquivo html:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Inserindo dados via JDBC com JSP</title>
</head>
<body>
<form action="inserir.jsp" method="post">
<table>
<tr>
<td>ISBN:</td><td><input type="text" name="isbn" /></td>
</tr>
<tr>
<td>Título:</td><td><input type="text" name="titulo" /></td>
</tr>
<tr>
<td>Edição:</td><td><input type="text" name="edicao" /></td>
</tr>
<tr>
<td>Publicação:</td><td><input type="text" name="publicacao" /></td>
</tr>
<tr>
<td>Descrição:</td>
<td><textarea name="descricao" rows="5" cols="25"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="btCadastrar" value="Enviar" />
</td>
</tr>
</table>
</form>
</body>

abaixo tenho o código do servlet NewServlet:

[code]public class NewServlet extends HttpServlet {
public void destroy(){
super.destroy();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String isbn = request.getParameter(“isbn”);
String titulo = request.getParameter(“titulo”);
String edicao = request.getParameter(“edicao”);
String publicacao = request.getParameter(“publicacao”);
String descricao = request.getParameter(“descricao”);
Connection conn = null;
PreparedStatement pst = null;
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
conn =
DriverManager.getConnection(“jdbc:mysql://localhost/livraria”,
“edson”,“integrator”);
String SQL = "INSERT INTO livros (isbn, titulo, edicao_num, " +
"ano_publicacao, descricao) " +
“values (?, ?, ?, ?, ?)”;
pst = conn.prepareStatement(SQL);
pst.setString(1, isbn);
pst.setString(2, titulo);
pst.setInt(3, Integer.parseInt(edicao));
pst.setString(4, publicacao);
pst.setString(5, descricao);
pst.executeUpdate();
pst.clearParameters();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
pst.close();
} catch (SQLException ex) {
Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
}
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void init()throws ServletException{
super.init();
}
}

[/code]

Qual o Erro da minha servlet?

agradeço desde ja

Quando você executou deu algum erro?!

vlw

DICA: ->> Recomendo, que a parte de conexão você criar um método ou até mesmo uma outra classe e passar os dados por GET SETs.
vlw
abs

Seu aplicativo está bem básico mesmo… muita coisa pra melhorar… :slight_smile:
mas para funcionar: Como você definiu a servlet no web.xml??? você fez o mapeamento dela??? no html você está acionando o “inserir.jsp”. tem que ter um servlet-mapping apontando de inserir.jsp para NewServlet. Tem isso?

o formulario html está chamando o inserir.jsp por que tava tentando com jsp pra ver se dava certu. mais tbm dá no mesmo.

Alguém teria um exemplo simples que possa me passar de como inserir dados no mysql com servlet?

preciso aprender.