簡體   English   中英

如何從html文本框在mysql表中插入記錄?

[英]How can I insert a record in the table of mysql from textboxes of html?

  1. 我有一個用於html的jsp文件和一個用於Java的myservlet.java文件。 我對在哪個文件中用jsp或Java編寫插入查詢感到困惑。

  2. 什么是netbean的插入查詢?是否正確?

    字符串查詢=“ INSERT INTO Teacher1(id,name)” +“ VALUES(” +1 +“,'” + aaa +“')”;

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>

我認為您的代碼不完整且比較復雜,因此這里很容易提供一種在數據庫中插入值的方法:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM