已成功插入值,但是如何以自动增量插入?

时间:2018-10-27 03:54:37

标签: mysql servlets jdbc

我的数据库:MYSQL SCREENSHOT

我已将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>

exception

我正在尝试从表单数据中插入数据,但是它不起作用。我该如何解决?

在这个更新的问题中,我已经成功解决了插入值时的一些问题,但是如何处理自动增量列?

1 个答案:

答案 0 :(得分:0)

button {
  width: 100%;
  height: 40px;
}

.selected {
  -webkit-appearance: none;
}

button:not(.selected) {
  display: none;
}

.dropdown:focus-within button {
  display: block;
}

并从准备好的字符串中删除ID