galera estou fazendo uma gravação no bd ate eu fiz certinho mais noa consigo trazer as informações e jogar na tela vou postar o codigo pra vcs verem
meu pacote bean
Classe Cliente
package Bean;
public class Cliente {
private String nome;
private String idade;
private String preco;
private String nota;
public Cliente() {
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getIdade() {
return idade;
}
public void setIdade(String idade) {
this.idade = idade;
}
public String getPreco() {
return preco;
}
public void setPreco(String preco) {
this.preco = preco;
}
public String getNota() {
return nota;
}
public void setNota(String nota) {
this.nota = nota;
}
}
Um servlets de controle
package Controle;
import Bean.Cliente;
import Percistencia.ClienteDao;
import Percistencia.Dao;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ControleCliente extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
Cliente cliente = new Cliente();
cliente.setNome(request.getParameter("nome"));
cliente.setIdade(request.getParameter("idade"));
cliente.setPreco(request.getParameter("preco"));
cliente.setNota(request.getParameter("nota"));
Dao dao = new Dao();
ClienteDao clientedao = new ClienteDao();
dao.open();
clientedao.grava(cliente);
out.print("Dados gravados!!");
}catch(Exception e){
out.print(e.getMessage());
}
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
Servlets de Controle e exibir dados do Banco
package Controle;
import Percistencia.ClienteDao;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ControleExibe extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
ClienteDao clientedao = new ClienteDao();
clientedao.open();
clientedao.exibe();
}catch(Exception e){
e.printStackTrace();
}
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
Classe de conecção Dao
package Percistencia;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Dao {
protected static Connection con;
protected PreparedStatement stmt;
protected ResultSet rs;
PrintWriter out;
public void open(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","meinel");
}catch(Exception e){
out.print(e.getMessage());
}
}
}
Classe de gravação dos dadosdo cliente
package Percistencia;
import Bean.Cliente;
import java.io.PrintWriter;
import java.sql.*;
public class ClienteDao extends Dao{
public ClienteDao() {
}
public void grava(Cliente cliente){
try{
stmt = con.prepareStatement("insert into cliente(nome,idade,preco,nota) values(?,?,?,?)");
stmt.setString(1,cliente.getNome());
stmt.setString(2,cliente.getIdade());
stmt.setString(3,cliente.getPreco());
stmt.setString(4,cliente.getNota());
stmt.execute();
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void exibe(){
try{
stmt = con.prepareStatement("select * from cliente");
rs = stmt.executeQuery();
while(rs.next()){
out.print("Nome: " +rs.getString("nome")+"<br>");
}
rs.close();
stmt.close();
con.close();
}catch(Exception e ){
e.printStackTrace();
}
}
}
bom gente é isso espro que vcs posssam me ajudar
abraço!!!