1.环境准备
1.1
druid-1.2.8.jar
commons-dbutils-1.7.jar
mysql-connectou-java-8.0.26.jar
servlet-api.jar
1.2 db.properties
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
url=jdbc:mysql:///db?serverTimezone=Asia/Shanghai
maxWait=60000
initialSize=100
maxActive=200
minIdle=10
1.3 DBUtils 工具类
private static DataSource ds = null;
private static final Properties PROPERTIES = new Properties();
static {
try {
PROPERTIES.load(DBUtils.class.getResourceAsStream("/db.properties"));
ds = DruidDataSourceFactory.createDataSource(PROPERTIES);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDs(){
return ds;
}
1.4 数据库--字段
public class User {
private Integer id;
private String username;
private String address;
private String password;
private String favorites;
private Integer gender;
private String grade;
}
2.实现登录功能
2.1 登录界面
<!-- form 表单如果没有指定 method ,默认为 get -->
<form action="/myweb03/doLogin" method="post">
<table>
<tr>
<td>用户名:</td>
<!--name 属性就是将来传递的 key,input 标签必须要有 name 属性,否则该字段无法被提交到服务端-->
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<!-- 点击 submit 按钮时, 会自动提交所有带 name 属性的数据-->
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>