Discuss / Java / 怎么越到后面就越没人交作业了。。。

怎么越到后面就越没人交作业了。。。

Topic source

叁木辛尧

#1 Created at ... [Delete] [Delete and Lock User]

1. 配置maven

<dependency>    <groupId>com.zaxxer</groupId>    <artifactId>HikariCP</artifactId>    <version>2.7.1</version></dependency>

2. UserService设置属性。记得生成get/set方法。

private HikariDataSource dataSource;

3. 配置application.xml

<bean id="userService" class="com.itranswrap.leanrjava.service.UserService">    <property name="mailService" ref="mailService" />    <property name="dataSource" ref="dataSource" /></bean><bean id="mailService" class="com.itranswrap.leanrjava.service.MailService" /><bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/javalearn" />    <property name="username" value="root" />    <property name="password" value="root" />    <property name="connectionTimeout" value="10000" />    <property name="idleTimeout" value="60000" />    <property name="maximumPoolSize" value="10" /></bean>

4. 随便写一个用例测试下

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.");    }}

深蓝738

#2 Created at ... [Delete] [Delete and Lock User]

可以输入代码块。

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.");
    }
}

叁木辛尧

#3 Created at ... [Delete] [Delete and Lock User]
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.");
    }
}


叁木辛尧

#4 Created at ... [Delete] [Delete and Lock User]

我记得我之前也是这么设置的,不知道为啥就乱了。

吞拿鱼958

#5 Created at ... [Delete] [Delete and Lock User]

这个HakariCP是你写的?

MacanRSG

#6 Created at ... [Delete] [Delete and Lock User]
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.");        }
    }
}

余建材

#7 Created at ... [Delete] [Delete and Lock User]

依赖:

``` 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>

```

@吞拿鱼958 这是一个类,JDBC连接池这一节有讲


  • 1

Reply