简体   繁体   中英

JSP - Connecting and Inserting data to MySQL Database

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!

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 aa single vertical and make that work, then move to the next.

Decomposition is the hallmark of computer science and problem solving.

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;

        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM