
hello friends, I have a problem in jsp.I want to insert data, which is given by user through a html page into a table.And the table name also is given by the user.My database is in mysql.I want to use placeholder("?") to insert data into table.Plsssss help me... Thanks in advance.

1)form.html:
<html> <form method="post" action="http://localhost:8080/examples/jsp/insertdata.jsp"> <table> <tr><td>Table Name:</td><td><input type="text" name="tname"></td></tr> <tr><td>Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)insertdata.jsp:
<%@page import="java.sql.*,java.util.*"%>
<%
String tname=request.getParameter("tname");
String name=request.getParameter("name");
String address=request.getParameter("address");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement pst=con.prepareStatement("insert into "+tname+"(name, address)values(?,?)");
pst.setString(1, name);
pst.setString(2, address);
pst.executeUpdate();
con.close();
out.println("Data is successfully inserted into database.");
}
catch(Exception e){
System.out.println(e);
}
%>
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.