简体   繁体   English

我正在尝试使用Java jsp将数据插入本地计算机的SQL数据库中

[英]I am trying to insert data in to a SQL database in my local computer using Java jsp

I am creating a web application in java jsp using Eclipse and Tomcat.but I am stock unable to insert the data in to my local SQL server. 我正在使用Eclipse和Tomcat在Java JSP中创建Web应用程序,但是我无法将数据插入到本地SQL服务器中。 The Form of the page is coded in HTML then I want the Java code part to get the data entered and insert the data in to the database but when I click the submit button absolutely nothing happens, no error message, no warning so far I am only able to type and clear the form. 页面的表单用HTML编码,然后我希望Java代码部分获取输入的数据并将数据插入数据库中,但是当我单击“提交”按钮时,绝对没有任何反应,没有错误消息,到目前为止没有警告只能输入和清除表格。 I am very new in Java. 我是Java的新手。 I just pick up this codding language recently but I am determine to learn it. 我最近才学习这种编码语言,但我决定学习它。

Help please here is my full code.


   <%@ page import="java.text.*,java.util.*" session="false"%>
   <%@ page import="java.sql.*" %>
   <%@page import="javax.swing.JOptionPane" %>
   <%@page import="java.util.Date" %>
   <%@page import ="java.io.IOException" %>
   <%@page import ="javax.servlet.ServletException" %>
   <%@page import ="javax.servlet.http.HttpServlet" %>
   <%@page import ="javax.servlet.http.HttpServletRequest" %>
   <%@page import ="javax.servlet.http.HttpServletResponse" %>
   <% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); %>

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert Laptops Data</title>
    <link rel="stylesheet" href="Style.css" type="text/css">
    </head>

    <body>

    <%!public class Insert extends HttpServlet {



        String dbURL = "jdbc:sqlserver://localhost\\SQLYRSIN";
        String user = "pass";
        String pass = "pass";
        Connection conn = null;
        PreparedStatement InsertLaptops = null;
        ResultSet resultSet = null;

        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {

            String LaptopMake, LaptopModel, LaptopServicetag, LaptopDatein, LaptopNotes, LaptopType;
            LaptopMake = req.getParameter("iMake");
            LaptopModel = req.getParameter("iModel");
            LaptopServicetag = req.getParameter("iServiceTag");
            LaptopDatein = req.getParameter("iDatein");
            LaptopNotes = req.getParameter("iNotes");
            LaptopType = req.getParameter("iType");

            try {

                conn = DriverManager.getConnection(dbURL, user, pass);

                Statement st = conn. createStatement();
                st.executeUpdate("INSERT INTO LaptopsTable (Make, Model,  [Service Tag], Datein, Notes, Type)"
                                + " VALUES  ('"+LaptopMake+"','"+LaptopModel+"','"+LaptopServicetag+"','"+LaptopDatein +"','"+LaptopNotes+"','"+LaptopType+"')");

                JOptionPane.showConfirmDialog(null, "Your Data Has been  Inserted", "Result", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.PLAIN_MESSAGE);
                st.close();
                conn.close();

            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }


    }%>
        <div id="header">
        <div class="logo">
            <a href="#"><span> Resident Screening</span> Jinventory </a>
        </div>
    </div>

    <div id="container">
        <div class="content">
            <h1>Add New Laptop</h1>

            <p>Make sure all Service Tag Enter in here are Laptops</p>

            <div id="box">
                <form name="LaptopsForm" method="get" class="contentfonts"
                    action="LaptopsInsert2.jsp" method="Post">
                    <table>
                        <tbody>

                            <tr>
                                <td>Service Tag</td>
                                <td><input type="text" name="iServiceTag" size="40"></td>
                            </tr>
                            <tr>
                                <td>Make</td>
                                <td><input type="text" name="iMake" size="40"></td>
                            </tr>
                            <tr>
                                <td>Model</td>
                                <td><input type="text" name="iModel"></td>
                            </tr>
                            <tr>
                                <td>Date</td>
                                <td><input type="date" name="iDate"></td>
                            </tr>
                            <tr>
                                <td>Type</td>
                                <td><input type="text" name="iTyped"  Value="Laptop"
                                    disabled="disabled"></td>
                            </tr>
                            <tr>
                                <td>Notes</td>
                                <td><input Type="text" name="iNotes" size="30" height="40"></td>
                            </tr>

                            <tr>
                                <td><input type="reset" name="Reset"></td>
                                <td><input type="submit" name="submit"></td>
                            </tr>
                        </tbody>
                    </table>
                </form>


            </div>

        </div>
    </div>

    </body>
    </html>

Here's a draft which you could use as a starting point. 这是草稿,您可以以此为起点。

As mentioned before, you should seriously consider some changes to your design: 如前所述,您应该认真考虑对设计的一些更改:

  • Separate View (.jsp) from Business-Logic (SQL-Statements etc.) 将视图(.jsp)与业务逻辑(SQL语句等)分开
  • Don't use scriptlets ( <% and so on ) 不要使用scriptlet(<%等)
  • Creating a Servlet within JSP is overkill - a JSP is compiled into a servlet by the servlet container. 在JSP中创建Servlet是多余的-Servlet容器将JSP编译为Servlet。
  • Don't create Connections one-by-one. 不要一一创建连接。 Use a connection pool. 使用连接池。 This will pay off quickly. 这将很快得到回报。

In the code you'll see some changes to your setup: 在代码中,您将看到对设置的一些更改:

  • form is submitted per POST 表格是根据POST提交的
  • instead of subclass, JSP checks for methods POST and only then tries the insert. JSP会检查方法POST,而不是子类,然后才尝试插入。
  • never ever use Swing inside Webapps. 永远不要在Webapp中使用Swing。 If that JOptionPane was opened, your webapp would block at that point, while the Dialog was shown somewhere on your server. 如果打开了该JOptionPane ,则此时对话框将显示在服务器上的某个位置,此时您的Web应用将被阻止。
  • Use of PreparedStatement instead of Statement this prevents SQL injection. 使用PreparedStatement代替Statement可以防止SQL注入。 You'll still want to check against Cross-Site-Scripting if you plan on displaying the inserted values in your webapp again. 如果您打算再次在Web应用程序中显示插入的值,您仍将需要对照跨站点脚本进行检查。

Disclaimer: I haven't tested or even tried to compile this code. 免责声明:我尚未测试甚至尝试编译此代码。 Edit was done only to point you in the right direction. 编辑只是为了使您指向正确的方向。

Here it goes 来了

<%@page import="java.text.*,java.util.*" session="false"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.Date" %>
<%@page import ="java.io.IOException" %>
<%@page import ="javax.servlet.ServletException" %>
<%@page import ="javax.servlet.http.HttpServlet" %>
<%@page import ="javax.servlet.http.HttpServletRequest" %>
<%@page import ="javax.servlet.http.HttpServletResponse" %>
<% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert Laptops Data</title>
<link rel="stylesheet" href="Style.css" type="text/css">
</head>

<body>

<%
if("POST".equals(request.getMethod()) {

  String dbURL = "jdbc:sqlserver://localhost\\SQLYRSIN";
  String user = "pass";
  String pass = "pass";
  Connection conn = null;
  PreparedStatement InsertLaptops = null;
  ResultSet resultSet = null;


  String LaptopMake, LaptopModel, LaptopServicetag, LaptopDatein, LaptopNotes, LaptopType;
  LaptopMake = req.getParameter("iMake");
  LaptopModel = req.getParameter("iModel");
  LaptopServicetag = req.getParameter("iServiceTag");
  LaptopDatein = req.getParameter("iDatein");
  LaptopNotes = req.getParameter("iNotes");
  LaptopType = req.getParameter("iType");

  try {

    conn = DriverManager.getConnection(dbURL, user, pass);

    PreparedStatement st = conn.prepareStatement("INSERT INTO LaptopsTable (Make, Model,  [Service Tag], Datein, Notes, Type) VALUES (?,?,?,?,?,?)");
    st.setString(1, LaptopMake);
    st.setString(2, LaptopModel);
    st.setString(3, LaptopServicetag);
    st.setString(4, LaptopDatein);
    st.setString(5, LaptopNotes);
    st.setString(6, LaptopType);

    st.executeUpdate();

    //This is SWING - and would at best popup a Dialog ON YOUR SERVER
    //JOptionPane.showConfirmDialog(null, "Your Data Has been  Inserted", "Result", JOptionPane.DEFAULT_OPTION,
    //        JOptionPane.PLAIN_MESSAGE);

    //Alternative: Show some HTML.                
    %>
    <h1>Your Data has been inserted.</h1>
    <%

    st.close();
    conn.close();

  } catch (SQLException ex) {
    ex.printStackTrace();
  }


} //END of Method.equals("POST")
%>
    <div id="header">
    <div class="logo">
        <a href="#"><span> Resident Screening</span> Jinventory </a>
    </div>
</div>

<div id="container">
    <div class="content">
        <h1>Add New Laptop</h1>

        <p>Make sure all Service Tag Enter in here are Laptops</p>

        <div id="box">
            <form name="LaptopsForm" method="POST" class="contentfonts"
                action="" method="POST">
                <table>
                    <tbody>

                        <tr>
                            <td>Service Tag</td>
                            <td><input type="text" name="iServiceTag" size="40"></td>
                        </tr>
                        <tr>
                            <td>Make</td>
                            <td><input type="text" name="iMake" size="40"></td>
                        </tr>
                        <tr>
                            <td>Model</td>
                            <td><input type="text" name="iModel"></td>
                        </tr>
                        <tr>
                            <td>Date</td>
                            <td><input type="date" name="iDate"></td>
                        </tr>
                        <tr>
                            <td>Type</td>
                            <td><input type="text" name="iTyped"  Value="Laptop"
                                disabled="disabled"></td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td><input Type="text" name="iNotes" size="30" height="40"></td>
                        </tr>

                        <tr>
                            <td><input type="reset" name="Reset"></td>
                            <td><input type="submit" name="submit"></td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</div>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试为我的 AP 计算机科学课程制作一个质数分解 Java 代码,但我被卡住了 - I am trying to make a Prime Factorization Java code for my AP Computer Science class and I am stuck 我在使用Java将数据插入mysql数据库时遇到问题 - I have problems to insert data into my mysql Database using Java 无法使用 Java/JSP 将数据插入 Apache Derby 数据库 - Unable to insert data into Apache Derby database using Java/JSP 如何使用Java Bean和jsp标签在Postgres数据库中插入数据? - How to insert data in Postgres database using Java Beans and jsp tags? 我想从数据库中检索图像并在JSP中使用显示它 <img> 标签。 我正在使用我的SQL - I want to retrieve image from database and display it in JSP using <img> tag. I am using my sql 我正在尝试使用Java更新oracle数据库中的密码。 难道我做错了什么? - I'm trying to update my password in oracle database using Java. Am I doing something wrong? 我正在尝试从数据库中提取数据并将其存储到 xml 文件中。 我正在使用 Sql 查询“Select * from”来提取数据 - I am trying to pull data from a database and store it into a xml file. I am using a Sql query "Select * from "for pulling data 使用servlet和jsp在数据库中插入5行数据 - insert 5 rows of data in the database using servlet and jsp 在eclipse中使用servlet和jsp将数据插入数据库 - insert data into database using servlet and jsp in eclipse 我是Java的新手,并且尝试连接到derby数据库。 我正在使用netbeans 7.4 - I am a newbie with java and I am trying to connect to a derby database. I am using netbeans 7.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM