
how can i get form input such as id convert it to int insert it into database

Hi,
You can get the parameter from the request object:
String s = request.getParameter("formfieldname");
Then convert it into Integer variable:
int i = Integer.parseInt(s);
Then you can write JDBC code to insert data into database.
Read JDBC Insert Statement Example for more detail.
Thanks

Hi Friend,
Try the following code:
1)form.jsp:
<%@page import="java.sql.*"%> <html> <form method="post" action="insertdata.jsp"> <table> <tr><td>Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Age:</td><td><input type="text" name="age"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)insertdata.jsp:
<%@page import="java.sql.*"%>
<%
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
String address=request.getParameter("address");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into data(name,age, address) values('"+name+"',"+age+",'"+address+"')");
con.close();
out.println("Data is successfully inserted into database.");
}
catch(Exception e){
System.out.println(e);
}
%>
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.