怎么越到后面就越没人交作业了。。。
Topic source可以输入代码块。
public User login(String email, String password) throws SQLException {
try (Connection conn = dataSource.getConnection()){
PreparedStatement ps = conn.prepareStatement("select * from user");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
Long dbId = rs.getLong("id");
String dbEmail = rs.getString("email");
String dbPassword = rs.getString("password");
String dbName = rs.getString("name");
if(dbEmail.equals(email) && dbPassword.equals(password)) {
return new User(dbId, dbEmail, dbPassword, dbName);
}
}
throw new RuntimeException("login failed.");
}
}
public User login(String email, String password) throws SQLException {
try (Connection conn = dataSource.getConnection()){
PreparedStatement ps = conn.prepareStatement("select * from user");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
Long dbId = rs.getLong("id");
String dbEmail = rs.getString("email");
String dbPassword = rs.getString("password");
String dbName = rs.getString("name");
if(dbEmail.equals(email) && dbPassword.equals(password)) {
return new User(dbId, dbEmail, dbPassword, dbName);
}
}
throw new RuntimeException("login failed.");
}
}
try (Connection conn = dataSource.getConnection()){
try(PreparedStatement ps = conn.prepareStatement("select * from user")){
try(ResultSet rs = ps.executeQuery()){
while(rs.next()) {
Long dbId = rs.getLong("id");
String dbEmail = rs.getString("email");
String dbPassword = rs.getString("password"); String dbName = rs.getString("name"); if(dbEmail.equals(email) && dbPassword.equals(password)) {
return new User(dbId, dbEmail, dbPassword, dbName); }
}
throw new RuntimeException("login failed."); }
}
}
依赖:
``` xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version></dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.1</version>
</dependency>
```
- 1
叁木辛尧
1. 配置maven
2. UserService设置属性。记得生成get/set方法。
3. 配置application.xml
4. 随便写一个用例测试下