第六周jsp作业

<html>
<head>
<title>登陆</title>
<meta charset="utf-8">
<link rel="icon" type="image/x-icon" href="../img/zrn.jpg" />
<link rel="stylesheet" type="text/css" href="../css/a2.css"/>
</head>
<body>
<h1 align="center">注册表单</h1>
<br>
<p style="text-align: center;color: red;font-size: 1.875rem;" >加入我们</p>
<form action="https://www.baidu.com" method="post" name="zrnfs">
<table>
<tr>
<td colspan="2">
<table class="table1">
<tr>
<td class="right">
用户名:
</td>
<td class="left">
<input type="text" maxlength="6" placeholder="请输入不超过6位的字符" required="required"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td class="right">
密&nbsp;&nbsp;&nbsp;码:
</td>
<td class="left">
<input type="password" maxlength="10" placeholder="请输入6-10位的数字与字母" required="required"/>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td class="right">
真实姓名:
</td>
<td class="left">
<input type="text">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td class="right">
确认密码:
</td>
<td class="left">
<input type="password"/>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td class="right">
手机号码:
</td>
<td class="left">
<input type="text" placeholder="请输入11位数字 "pattern="[0-9]{11}"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<table class="table1">
<tr>
<td class="right">
性&nbsp;&nbsp;&nbsp;别:
</td>
<td class="left">

<input type="radio" name="sex" value="男" checked="checked"/>男
<input type="radio" name="sex" value="女" />女
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td class="right">
民&nbsp;&nbsp;族:
</td>
<td class="left">
<select name="minzu[]"size="1">
<option value ="汉">汉族</option>
<option value ="满">满族</option>
<option value ="蒙古">蒙古族</option>
<option value ="维吾尔">维吾尔族</option>
</select>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td class="right">
邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱:
</td>
<td class="left">
<input type="email" name="email" maxlength="15"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<table class="table1">
<tr>
<td align="right">
爱&nbsp;&nbsp;&nbsp;好:
</td>
<td>
<input type="checkbox" name="basketball"/>篮球
<input type="checkbox" name="football"/>足球
<input type="checkbox" name="wushu"/>武术
<input type="checkbox" name="swimming"/>游泳
</td>
</tr>
</table>
</td>

</tr>
<tr>
<td>
<table>
<tr>
<td class="right">
国&nbsp;&nbsp;&nbsp;家:
</td>
<td class="left">
<select name="guojia[]"size="2" multiple>
<option value="a">中国</option>
<option value="b">美国</option>
<option value="c">日本</option>
<option value="d">德国</option>
<option value="e">澳洲</option>
<option value="f">加拿大</option>
</select>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td class="right">
城&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;市:
</td>
<td class="left">
<select name="home[]"size="2" multiple>
<optgroup label="东北">
<option value="沈阳">沈阳</option>
<option value="大连">大连</option>
<option value="盘锦">盘锦</option>
</optgroup>
<optgroup label="西南">
<option value="四川">重庆</option>
<option value="贵州">遵义</option>
<option value="云南">大理</option>
</optgroup>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr height="100">
<td align="center" valign="bottom">
提交<button type="submit" value="提交" style="border-radius: 50%;overflow: hidden;border: none;width: 50px;height: 50px;padding: 0;"><img src="zrn.jpg" width="50px"height="50px"></button>
</td>
<td align="center" valign="bottom">
重置<button type="reset" value="重置" style="border-radius: 50%;overflow: hidden;border: none;width: 50px;height: 50px;padding: 0;"><img src="zrn2.jpg"width="50px"height="50px" ></button>
</td>
</tr>
</table>
</form>

</body>
</html>

package com.bjpowrenode.model.dao;
// Dao 数据库实体化类

import java.sql.*;

public class UserDao {
//insert 增加数据 sql语句是
//insert into user(id,username,password,name)values(?,?,?,?)
public void insertUser(User user) {
//参数是User对象
//完成用户添加的数据库持久化方法

// 1.加载驱动,通知JDBC要连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

//2.获取连接对象,连接到数据库
Connection connection=null;
PreparedStatement preparedStatement= null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/data1?useUnicode=true&characterEncoding=UTF-8","root","200031");
//在数据库名字后面加上?useUnicode=true&characterEncoding=UTF-8 将乱码转换成汉字
//参数1数据库的链接路径 jdbc:mysql//协议名,localhost为需要连接的数据库所在ip地址,localhost表示本机
// 3306为数据库的端口号 data1表示所需要连接的具体的数据库的库名
//参数2 连接数据时候的用户名 参数3连接数据时用户名的密码

// 定义SQL语句
String sql="insert into user(id,username,password,name)values(?,?,?,?)";//?是占位符需要后期动态赋值

//3.定义sql容器并装载我们的sql语句
preparedStatement = connection.prepareStatement(sql);

//4.为语句中的?赋值可选操作步骤如果语句中没有?不需要赋值
//参数1表示第几个问号
// set数据类型()根据数据库中字段的具体类型调用对应的方法 varchar对应string int对应int bigint对应Long
// 有几个问号就需要有几行赋值并且序号要与?一一对应
preparedStatement.setLong(1,user.getId());
preparedStatement.setString(2, user.getUsername());
preparedStatement.setLong(3,user.getPassword());
preparedStatement.setString(4,user.getName());

//5.执行sql语句
preparedStatement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}


}
//update 修改数据库属性
// sql语句是update user set username=?,name=?,password=?,id=?
public void updateUser(User user) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection=null;
PreparedStatement preparedStatement= null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/data1?useUnicode=true&characterEncoding=UTF-8","root","200031");
String sql="update user set username=?,name=?,password=?,id=?";//?是占位符需要后期动态赋值
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setLong(4,user.getId());
preparedStatement.setString(1,user.getUsername());
preparedStatement.setLong(3,user.getPassword());
preparedStatement.setString(2,user.getName());
preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}

//数据库删除数据
//sql语句是delete form user where id=?
public void deleteUserById(Long id) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection=null;
PreparedStatement preparedStatement= null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/data1?useUnicode=true&characterEncoding=UTF-8","root","200031");
String sql="delete form user where id=?";//?是占位符需要后期动态赋值
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setLong(1,id);
preparedStatement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}

posted @ 2021-04-12 13:02  学习不错  阅读(26)  评论(0编辑  收藏  举报