Reputation: 7288
Right, here's my situation, I have a form on a page (index.jsp) which submits data to a JSP page (output.jsp) which will then do things with the data and output a result. I would like to insert the results of this into a MySQL database.
I've tried doing some things with MySQL directly in the JSP but I've been advised against this as it opens security holes, I believe.
So I have created a SubmitServlet.java (by default, Netbeans has put it into Source Packages/default package, opposed to the location of the JSP files, in Web Pages). I understand that I've got to do a lot of the connecting here, though I'm not entirely sure how to get the data submitted into the database from the JSP, to the servlet. What do I need to do?
I have read several tutorials at RoseIndia, but they've only just left me more confused as to what is actually required so any help would be appriciated greatly!
PS: Also worth pointing out I am completely new to Java web applications, so the more you treat me like a complete idiot, the better!
Upvotes: 0
Views: 5012
Reputation: 4387
call the required method from output.jsp which will insert the data into the database
<%
<jsp:useBean id="obj" class="yourPackage.className" />
String Success="";
Success=sobj.myRequiredMethod("this is my result");
%>
in yourPackage.className class u should have the myRequiredMethod()
package yourPackage;
import java.sql.*;
import java.util.*;
public class className
{
String mySqlPassword="root";
String mySqlUsername="root";
String mySqlDatabase="jdbc:mysql://localhost:3306/yourDatabse";
ResultSet res;
public String myRequiredMethod(String result)
{
String success="";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(mySqlDatabase,mySqlUsername,mySqlPassword);
String q="insert into myTable (resultColumn) values('" + result + "')";
System.out.println(q);
PreparedStatement stmt = con.prepareStatement(q);
int pos = stmt.executeUpdate();
System.out.println(pos);
if(pos==1)
{
success="Successfull";
}
}
catch(Exception e)
{
}
return success;
}
}
Upvotes: -1
Reputation: 308763
Rose India's not a good place to learn.
No, you need an intermediate servlet or two.
Break the problem into chunks: schema, CRUD operations, mid-tier models, service, marshalling HTTP requests into objects and back, HTML and CSS and JavaScript. Don't try to do the whole thing at once. Start with a a single vertical and make that work, then move to the next.
Decomposition is the hallmark of computer science and problem solving.
Upvotes: 2