java - 如何从html文本框在mysql表中插入记录?

标签 java mysql database jsp web

我有一个用于html的jsp文件和一个用于Java的myservlet.java文件。我对于在jsp或Java中将在哪个文件中编写插入查询感到困惑?
什么是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;
}

关于java - 如何从html文本框在mysql表中插入记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422884/

相关文章:

mysql - 从 shell 将多个 .sql 转储文件导入 mysql 数据库

java - 在 Android 应用程序中存储文本数据的最佳方式?

sql - SQL 数据库设计初学者指南

java - ArrayList 对象在 java android 的 ListView 上返回一行

Java程序与网络连接超时

php - 读取 Soap Response XML 并插入到 mysql 中

php - 在 mySQL 中显示所有日期,甚至包括零数据的日期?

database - 有哪些出色的在线数据库建模工具?

java - Tomcat webapps 服务器独立吗?

java - 使用 SWIG 将 Java Map<String, String> 传递给 C++ 方法