第五次作業
index.jsp
<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>
UserBean.jsp
package com.media.bean;
public class UserBean {
private String username;
private String password;
private int gender;
private int age;
private int status;
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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
register.jsp
<%
UserBean user = new UserBean();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
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);
Statement stmt = connection.createStatement();
String sql = "insert into user (username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
//String sql = "update user set status = 3 where id = 2";
//String sql = "delete from user where id = 4";
int count = stmt.executeUpdate(sql);
if(count == 0 ){
out.print("註冊失敗");
}else{
out.print("註冊成功");
}
%
index
<form action="home.jsp " method="post">
<input type ="text " name="username">
<input type ="text " name="password">
<button type ="submit ">提交</button>
</form>
home.jsp
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user where username = ? and password = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,request.getParameter("username"));
ps.setString(2,request.getParameter("password"));
ResultSet rs = ps.executeQuery();
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
/*
Statement stmt = connection.createStatement();
String sql = "select * from user";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getNString(3)+"<br>");
}
*/
%>
相關文章
- C++第五次作業C++
- 埋是土裡埋的第五次作業
- 高階語言程式設計第五次作業程式設計
- 高階語言程式設計第五次個人作業程式設計
- 高階語言程式設計課程第五次個人作業程式設計
- 高階語言程式設計第五次個人作業(102400106 劉鑫語)程式設計
- 團隊作業第五次—專案系統設計與資料庫設計資料庫
- 第五次
- 第五次衝刺
- 第五次實驗
- 作業系統 作業5作業系統
- 作業
- python第五次筆記---物件導向Python筆記物件
- 【團隊作業】第三週作業1
- 作業系統(一):作業系統概述作業系統
- 作業系統(1)——作業系統概述作業系統
- 作業四
- 作業三
- 作業二
- 作業01
- 作業一
- 作業1
- python作業Python
- 作業2
- 2024.6.27作業
- 作業7
- 2024.6.24作業
- 2024.6.20作業
- 2024.6.21作業
- 2024.3.15作業
- 2024.3.14作業
- 2024.3.13作業
- 2024.3.12作業
- 2024.4.25作業
- 2024.4.28作業
- 2024.4.12作業
- 2024.4.16作業
- 2024.5.7作業