如何从html的文本框中插入mysql表中的记录?

时间:2016-07-17 15:31:17

标签: java mysql database jsp web

  1. 我有一个用于Java的html和myservlet.java文件的jsp文件。我很困惑在哪个文件中我将在jsp或Java中编写插入查询?

  2. 什么是netbean的插入查询?是否正确?

    String query =“INSERT INTO teacher1(id,name)”+“VALUES(”+ 1 +“,”“+ aaa +”')“;

  3. myservlet.java

    class dbconn {
    
        Connection c1 = null;
        Statement st = null;
        ResultSet rs = null;
        private final String ac;
        private final String aaa;
    
        dbconn() {
    
            try {
                Class.forName("com.mysql.jdbc.Driver");
                c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher", "root", "abcde");
    
            } catch (Exception cnfe) {
                System.out.println("Couldn't find the driver!");
                System.out.println("Couldn't connect: print out a stack trace and exit.");
                System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
            }
            try {
                st = (Statement) c1.createStatement();
                System.out.println("Statement Created Successfully");
            } catch (SQLException se) {
                System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
    
            }
            if (c1 != null) {
                System.out.println("Hooray! We connected to the database!");
            } else {
                System.out.println("We should never get here.");
            }
            String query = "INSERT INTO teacher1 (id,name) "
                    + "VALUES (" + 1 + ", '" + aaa + "')";       // insert query is this
        }
    
        protected void processRequest(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
    
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet myservlet</title>");
                out.println("</head>");
                out.println("<body >");
    
                out.println("<h1>Servlet myservlet at " + request.getContextPath() + "</h1>");
                String name = request.getParameter("firstname");
                out.println("<h2>" + name + "</h2>");
                out.println("</body>");
                out.println("</html>");
            } finally {
                out.close();
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        public String getServletInfo() {
    
            return "Short description";
    
        }
    }
    

    的index.jsp

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
    
            <form action="myservlet"  method="GET"    >
    
                First name:<br> 
                <input type="text" name="firstname"/> 
                <br> 
                Last name:
                <br> 
                <input type="text" name="lastname" /> 
                <br>  <br> 
                <input type="submit" value="Submit"/> 
            </form>
        </body>
    </html>
    

1 个答案:

答案 0 :(得分:0)

我认为您的代码不完整且很复杂,所以这里很容易在数据库中插入值:

public boolean insert(String firstname, String lastname) throws SQLException, ClassNotFoundException {
    //Connection to database
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher", "root", "abcde");
    boolean succes = false;

    PreparedStatement statement = null;
    try {

        //Create a prepared statement
        String requete = "INSERT INTO teacher1 (fname,lname) VALUES (?, ?)";

        statement = connection.prepareStatement(requete);

        statement.setString(1, firstname);
        statement.setString(2, lastname);
        //Excute the insertion
        int r = statement.executeUpdate();

        //Test the insertion is correct or no
        if (r == 1) {
            succes = true;
        }

    } catch (SQLException e) {
        System.out.println("Exception :" + e);
    } finally {
        //Close your connection
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }

    return succes;
}