java1996 2016-11-15 03:05 采纳率: 0%
浏览 1870

写了一个注册页面,数据库id是主键但是要自动生成id,现在只能生成id为1,怎么按顺序生成id

 <%@page import="com.sun.corba.se.spi.orbutil.fsm.Guard.Result"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import= "edu.jmi.db.*" %>
<%@ page import= "edu.jmi.dao.*" %>
<%@ page import= "edu.jmi.model.*" %>
<%@ page import= "java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style type="text/css">
</style>
<body>
<center>
<%
//1设置解码
    request.setCharacterEncoding("utf-8");
//2获取参数
    String str=request.getParameter("name");
    String str1=request.getParameter("password");
    String str2=request.getParameter("password2");
    String str3=request.getParameter("mail");
    System.out.println(str+str1+str2+str3);
    if(str==null){//如果初次加载页面,直接返回,不会执行下面存取数据库的代码
         //return;
    }else{
        session.setAttribute("usr", str);
        response.sendRedirect("regok.jsp"); 

//3存取数据库
//3.1获取数据库链接
    Connection conn=DB.getConnection();
//3.2准备插入sql语句
    String sql="insert into user values(?,?,?,?)";
    PreparedStatement pst=conn.prepareStatement(sql);
    pst.setInt(1, 1); //id
    pst.setString(2,str);//name
    pst.setString(3,str1);//pass
    pst.setString(4,str3);//email
//3.3执行插入
    int res=pst.executeUpdate();
    System.out.println("插入结果是:"+res);
    pst.close();
    conn.close();
    }

%>
<form action="" >
         <p>用户名:<input type="text" value="" name="name"></p>           
         <p>密码:<input type="password" value="" name="password"></p>               
         <p>确认密码:<input type="password" value="" name="password2"></p>
         <p>邮箱:<input type="text" value="" name="mail"></p>              
                <input type="submit"  value="注册">
                <input type="reset"  value="重置">
</form>
</center>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • zilin-lynn 2016-11-15 03:05
    关注

    在数据库中配置id为自增长,然后insert sql 就不需要带id

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗