Sql与JSP链接浅试

本文通过实例代码展示了如何使用Java JSP连接SQL数据库,包括数据库连接的bean,注册页面处理,主函数以及注册结果的判断。配合截图,帮助读者理解操作流程。
摘要由CSDN通过智能技术生成

代码

与数据库连接的bean

package com.media.bean;

public class UserBean {
  private String username;
  private String password;
  private String gender;
  private int age;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
}

index 注册页

<%--
  Created by IntelliJ IDEA.
  User: 了
  Date: 2020/9/23
  Time: 9:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>注册</title>
  </head>
  <body>

  <form method="post" action="register.jsp">
   用户名: <input type="text" name="username"><br>
   密码: <input type="text" name="password"><br>
   性别: <input type="text" name="gender"><br>
   年龄: <input type="text" name="age"><br>
    <button type="submit">注册</button>
  </form>
  </body>
</html>

main函数

<%--
  Created by IntelliJ IDEA.
  User: 了
  Date: 2020/9/23
  Time: 9:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="request"></jsp:useBean>
<%
out.print(user.getUsername());
%>
</body>
</html>

register 注册成功否

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
  Created by IntelliJ IDEA.
  User: 了
  Date: 2020/9/23
  Time: 9:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="application"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//UserBean user1=new UserBean();
    //user.setUsername(request.getParameter("username"));
    //out.print(user.getUsername());

    //加载驱动程序
    Class.forName("com.mysql.jdbc.Driver");

    //建立数据库连接
    String url = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";

    Connection connection = DriverManager.getConnection(url);//获得链接对象

    String sql = "INSERT INTO user (username,password,gender,age) VALUES('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";

    Statement stmt = connection.createStatement();

    int count = stmt.executeUpdate(sql);

    if(count == 0){
        out.print("注册失败");
    }else {
        out.print("注册成功");
    }

%>
<jsp:getProperty name="user" property="username"/>
</body>
</html>

截图

注册
组测成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值