已经成功插入了值,但是,如何用autoincrement插入?

31moq8wy  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(344)

我的数据库:mysql截图
我已经将html中的值插入mysql数据库。如何对自动增量列使用insert命令?
servlet代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.sql.*;

@WebServlet(name = "Register")
public class Register extends HttpServlet {
static String username="root";
static String password="root";
static String dburl= "jdbc:mysql://localhost:3306/conferencesystem";
static String mydriver = "com.mysql.cj.jdbc.Driver";
protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String Firstname = request.getParameter("firstname");
    String Lastname = request.getParameter("lastname");
    int Mobilenumber = 
Integer.parseInt(request.getParameter("mobilenumber"));
    String Email = request.getParameter("emailid");
    String Pass = request.getParameter("password");
    out.println("<html>");
    out.println("<body>");
    out.println("<h3>Guest Details </h3>");
    out.println("" + Firstname);
    out.println("" + Lastname);
    out.println("" + Email);
    out.println("</body>");
    out.println("</html>");

    int id = 1101;

    Connection con = null;
    try {
        Class.forName(mydriver);
        con = DriverManager.getConnection(dburl, username, password);
        String query = "INSERT INTO author values(?,?,?,?,?,?)";

        PreparedStatement stmt = con.prepareStatement(query);

        stmt.setInt(1,id);

        stmt.setString(2,Firstname);

        stmt.setString(3,Lastname);

        stmt.setInt(4,Mobilenumber);

        stmt.setString(5,Email);

        stmt.setString(6,Pass);
        out.println("Your Record has been successfully inserted");
        int res= stmt.executeUpdate();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
h3{
    font-family: Calibri;
    font-size: 25pt;
    font-style: normal;
    font-weight: bold;
    color:SlateBlue;
    text-align: center;
    text-decoration: underline
}

table{
    font-family: Calibri;
    color:white;
    font-size: 11pt;
    font-style: normal;
    font-weight: bold;
    text-align:center;
    background-color: SlateBlue;
    border-collapse: collapse;
    border: 2px solid navy
}
table.inner{
    border: 0px
}
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">

<html>
<head>
    <title> Author Registration Form </title>

</head>
<link rel="stylesheet" href="styles.css">
<body>
<form method="post" action="http://localhost:8080/Register">
    <h3>AUTHOR REGISTRATION FORM</h3>

    <table align="center" cellpadding = "10">

        <!----- First Name ---------------------------------------------------------->
        <tr>
            <td>FIRST NAME</td>
            <td><input type="text" name="firstname" maxlength="30"/>
                (max 30 characters a-z and A-Z)
            </td>

        </tr>

        <!----- Last Name ---------------------------------------------------------->
        <tr>
            <td>LAST NAME</td>
            <td><input type="text" name="lastname" maxlength="30"/>
                (max 30 characters a-z and A-Z)
            </td>
        </tr>

        <!----- Mobile Number ---------------------------------------------------------->
        <tr>
            <td>MOBILE NUMBER</td>
            <td>
                <input type="text" name="mobilenumber" maxlength="10" />
                (10 digit number)
            </td>
        </tr>

        <!----- Email Id ---------------------------------------------------------->
        <tr>
            <td>EMAIL ID</td>
            <td><input type="text" name="emailid" maxlength="100" /></td>
        </tr>

        <!----- Choose password ---------------------------------------------------------->
        <tr>
            <td>PASSWORD</td>
            <td><input type="password" name="password" maxlength="100" /></td>
        </tr>
        <!----- Submit and Reset ------------------------------------------------->
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Submit">
                <input type="reset" value="Reset">
            </td>
        </tr>
    </table>

</form>

</body>
</html>


我试图从表单数据中插入数据,但它不起作用。我该怎么修?
在这个更新的问题中,我成功地解决了插入值时的一些问题,但是如何处理自动递增列呢?

ovfsdjhp

ovfsdjhp1#

INSERT INTO author
 (firstname
 ,lastname
 ,mobilenumber
 ,email 
 , pass) values(?,?,?,?,?);

从准备好的字符串中删除id

相关问题