JSP (Java Server Page) - cast y reload

 
Vista:
sin imagen de perfil

cast y reload

Publicado por Adrian (10 intervenciones) el 28/02/2007 03:03:29
hola a todos:
tengo el siguiente problema estoy tratando de insertar un registro en mi base de datos con un jsp y un servlet. pero el CountryId y integer y no se como castearlo el codigo es.. lo otro que necesito es que cuando termine de actualizar la base, llame al mismo jsp con los datos.. se que la conexion la tengo que hacer en un servlet separado pero por ahora estoy probando

gracias

Adrian

JSP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
...
<FORM method="get" action="mypackage.Country">

<%@page
import = "java.io.*"
import = "java.sql.*"
import = "java.lang.*"
import = "javax.servlet.*"
import = "javax.servlet.jsp.*"
import = "javax.servlet.http.*"
import = "mypackage.Conexion"
%>

<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Country Id</td>
<td><INPUT type=text name="CountryId" maxlength=20> </td>
</tr>
<tr>
<td>Name</td>
<td><INPUT type=text name="Name" maxlength=20> </td>
</tr>
</table>

</Form>...

y EL SERVLET ES :
public class Country extends HttpServlet {

static String login = "root";
static String password = "";
static String url = "jdbc:mysql://localhost/ryersonbooks";


public void service(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException
{

// Sacar los Datos de la Forma
String CountryId = request.getParameter("CountryId");
String Name = request.getParameter("Name");

Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,login,password);

if (conn != null)
{
Statement stmt = conn.createStatement();
System.out.println("conexion ok");

String sql_inserta = "INSERT INTO Countrys(CountryId,Name) VALUES ( ? , ? )";
PreparedStatement statement = conn.prepareStatement(sql_inserta);

statement.setInt(1, (Integer)request.getAttribute("CountryId"));
statement.setString(2,(String) request.getAttribute("Name"));

statement.executeUpdate();
statement.close();
}
}
catch(SQLException ex)
{
System.out.println(ex);
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder