简体   繁体   中英

How do I allow user to add data to MySql database from JSP page in Netbeans

I'm having some difficulty with getting user input into the mysql database from a jsp page. I am currently working in netbeans and have a table with the following inputs:

containerinventory
ID (int)
containerNumber(varchar)
FullOut(date)
EmptyIn(date)
EmptyOut(date)
FullIn(date)
Comments(varchar)

So I want to set up a form on my jsp page to allow users to add an item to inventory and input all the relevant information for all the fields listed in the above table.

I've read about using PHP, but it seems like all of my PHP code when I try it on this page is commented out and not read. I'm using

I know that I am connected to the DB because I've put a SQL command to display everything in the DB, and I've added to the DB from the development side to verify.

How can I allow user input from a form to input into this database? Any help would be greatly appriciated. I'd like the user to be able to enter all relevant information in a text box, click submit and have it added to the inventory.

I'm not too familiar with PHP or JSP, so please be gentle :) Thanks for any help. I know how to use a form within this code, and I've tried, but it does not pass through to the DB.

So here is my code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>


<sql:query var="result" dataSource="jdbc/rukerttracker">
        SELECT * FROM containerinventory
   <? <div id="indexLeftColumn">
        <div id="welcomeText">
            <p>[ Welcome Text ]</p>

      <!-- test to access context parameters -->
    categoryImagePath: ${initParam.categoryImagePath}
    productImagePath: ${initParam.productImagePath}
</div>
        <div style="height:50%;overflow:auto;">
        <table border="1">
    <!-- column headers -->
    <tr>
    <c:forEach var="columnName" items="${result.columnNames}">
        <th><c:out value="${columnName}"/></th>
    </c:forEach>
</tr>
<!-- column data -->
<c:forEach var="row" items="${result.rowsByIndex}">
    <tr>
    <c:forEach var="column" items="${row}">
        <td><c:out value="${column}"/></td>
    </c:forEach>
    </tr>
</c:forEach>`enter code here`

In your HTML Page,

Create a form with input boxes and a submit button. I suggest using a servlet to input values in the database. However, just to get a basic concept across:

   <form name="sample" action="insert.jsp">
   <label> Text 1</label><input type="text" name="txt1" value=""/>
   <label> Text 2</label><input type="text" name="txt2" value=""/>
    <input type="submit" value="Enter Values"/>

now in insert.jsp

           <%@ page import="java.sql.*" %>
           <%
           Connection conn = null;
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/nikita","root","123");
           String t1=request.getParameter("txt1");
           String t2=request.getParameter("txt2");

int rss=stmt.executeUpdate("insert into tablename values('"+t1"','"+t2"'));
            if(rss>0){ 
                 out.print("INSERTED");
            }
           else
            {
            out.print("TRY AGAIN");
            }

Don't forget to add the required JAR files.

Cheers!

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