写一个javaweb代码,在jsp页面提交个人信息和账号密码后,将id、姓名、账号、密码都存储在数据表里,数据库名是stu,数据表名为stu_account,账号对应的属性为account_number,密码对应的属性为password,姓名对应的属性为name,id对应的属性为id,数据库名为root,数据库登录密码为BAIWU2002.
时间: 2024-03-19 19:42:52 浏览: 25
好的,这里提供一个简单的实现方法,需要使用到JDBC连接数据库,以下是代码实现:
在JSP页面中,可以使用表单提交用户信息和账号密码:
```html
<form action="register.jsp" method="post">
<label>姓名:</label>
<input type="text" name="name"><br>
<label>账号:</label>
<input type="text" name="account"><br>
<label>密码:</label>
<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
```
在register.jsp中,可以接收表单提交的数据,并将其存储到数据库中:
```java
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name");
String account = request.getParameter("account");
String password = request.getParameter("password");
// JDBC连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu?useSSL=false", "root", "BAIWU2002");
Statement stmt = conn.createStatement();
// 插入数据
String sql = "INSERT INTO stu_account (name, account_number, password) VALUES ('" + name + "', '" + account + "', '" + password + "')";
stmt.executeUpdate(sql);
// 关闭连接
stmt.close();
conn.close();
// 输出成功信息
out.print("注册成功!");
} catch (Exception e) {
out.print("注册失败:" + e.getMessage());
}
%>
```
需要注意的是,此代码仅供参考,实际应用中需要进行更严谨的数据校验和防止SQL注入等安全措施。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)