ADEMILTON 24 de mai. de 2012
O método executeQuery é para obter retorno de linhas. O correto seria usar o executeUpdate.
Vale outro alerta: estás emendando as strings “UPDATE condutores” com “SET cpf=’” … isso vai dar zebra… tem que ter um espaço pelo menos no final da primeira string…
Mas o melhor de todos os mundos seria um PrepareStatement.
bacoco 24 de mai. de 2012
ADEMILTON:
O método executeQuery é para obter retorno de linhas. O correto seria usar o executeUpdate.
Vale outro alerta: estás emendando as strings "UPDATE condutores" com "SET cpf='" ... isso vai dar zebra... tem que ter um espaço pelo menos no final da primeira string...
Mas o melhor de todos os mundos seria um PrepareStatement.
st.executeUpdate("UPDATE condutores " +
"SET cpf='"+cpf+"', cnpj='"+cnpj+"', condutor='"+condutor+"', carta='"+carta+"', numerohab='"+nhab+"'" +
" WHERE cpf = '"+pesquisa+"';" );
Também não funciona... \:
Como seria usar o PrepareStatement?
Ele inteiro sei que está meio confuso...
<% @ page import = "java.util.*" %>
<% @ page import = "java.sql.*" %>
<% @ page language = "java" contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title > Alterar Condutor</ title >
< h1 > Alterar Condutor</ h1 >
</ head >
< body background = "images/background1.jpg" >
< form method = "post" >
Digite o CPF do condutor que deverá ser alterado:
< input type = "text" name = "pesquisa1" >
</ form >
< form method = "post" >
< table border = "2" cellpadding = "1" cellspacing = "1" >
< tr height = "50%" >
< td >
< Table border = "1" >
< tr >
< td colspan = "2" >
CPF:< input type = "text" size = "45" name = "cpf" >
</ td >
</ tr >
< tr >
< td >
CNPJ: < input type = "text" size = "20" name = "cnpj" maxlength = "20" >
</ td >
</ tr >
< tr >
< td colspan = "2" >
Condutor: < input type = "text" name = "condutor" size = "40" >
</ td >
< td align = "center" colspan = "2" >
Tipo de Habilitação:
< select name = "carta" >
< option value = "A" > A</ option >
< option value = "B" > B</ option >
< option value = "C" > C</ option >
< option value = "D" > D</ option >
< option value = "E" > E</ option >
</ select >
< td colspan = "1" align = "center" >
Numero da Habilitação:
< input type = "text" name = "nhab" size = "21" >
</ td >
</ tr >
</ Table >
</ td >
</ tr >
</ table >
< br >
< br >
< input type = "reset" name = "Reset" value = "Apagar" >
< input type = "button" name = "Voltar" value = "Voltar" Onclick = "window.location ='Homepage.jsp';" >
< input type = "submit" name = "Submit" value = "Concluir" >
<%
String pesquisa = request . getParameter ( "pesquisa1" );
String cpf = request . getParameter ( "cpf" );
String cnpj = request . getParameter ( "cnpj" );
String condutor = request . getParameter ( "condutor" );
String carta = request . getParameter ( "carta" );
String nhab = request . getParameter ( "nhab" );
Connection con = null ;
Statement st = null ;
ResultSet res = null ;
try
{ Class . forName ( "org.postgresql.Driver" );
con = DriverManager . getConnection
( "jdbc:postgresql://localhost:5432/postgres/veiculos" , "postgres" , "aluno" );
st = con . createStatement ();
// ResultSet rs = st . execute ( "UPDATE condutores" +
// "SET cpf='" + cpf + "', cnpj='" + cnpj + "', condutor='" + condutor + "', carta='" + carta + "', numerohab='" + nhab + "'" +
// "WHERE cpf = '" + pesquisa + "';" );
st . executeUpdate ( "UPDATE condutores " +
"SET cpf='" + cpf + "', cnpj='" + cnpj + "', condutor='" + condutor + "', carta='" + carta + "', numerohab='" + nhab + "'" +
" WHERE cpf = '" + pesquisa + "';" );
//ou t . println ( "alteração feita com sucesso" );
}
catch ( ClassNotFoundException cnfex )
{ out . println ( "Falha ao carregar o driver" );
}
catch ( SQLException sqlex )
{ out . println ( "Erro " + sqlex );
}
%>
</ form >
</ body >
</ html >
ADEMILTON 27 de mai. de 2012
Ficaria assim:
PreparedStatement pstm = con . prepareStatement ( "update condutores set cpf = ?, cnpj = ?, condutor = ?, carta = ?, numerohab = ? where cpf = ?" );
pstm . setString ( 1 , request . getParameter ( "cpf" ));
pstm . setString ( 2 , request . getParameter ( "cnpj" ));
pstm . setString ( 3 , request . getParameter ( "condutor" ));
pstm . setString ( 4 , request . getParameter ( "carta" ));
pstm . setString ( 5 , request . getParameter ( "nhab" ));
pstm . setString ( 6 , request . getParameter ( "pesquisa1" ));
pstm . execute ();