201819101001張佳興

18教技張佳興發表於2020-09-30
<%@ 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>
 
<%@ page import="com.media.bean.User" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
  Created by IntelliJ IDEA.
  User: 官方
  Date: 2020/9/23
  Time:  9:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" language="java"%>

<jsp:useBean id="user" class="com.media.bean.User" ></jsp:useBean>

<jsp:setProperty name="user" property="*"></jsp:setProperty>


<%
    //載入驅動程式
    Class.forName("com.mysql.jdbc.Driver");

    //建立資料庫連線
    String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";//book 為資料庫名

    Connection connection = DriverManager.getConnection(url);//獲得連線物件 alt+ 回車 自動匯入類

    //執行sql語句
    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("註冊成功");

    }
%>

在這裡插入圖片描述
在這裡插入圖片描述

相關文章