package review_javaweb.demo01;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class Practice {
private Statement statement;
private Connection connection;
private Properties properties;
@Test
public void test01() throws SQLException {
List<Account> accountList = getAccountList();
accountList.forEach(item->{
System.out.println(item);
});
}
private List<Account> getAccountList() throws SQLException {
List<Account> accountList = new ArrayList<>();
String sql = "select * from account";
ResultSet resultSet = statement.executeQuery(sql);
Account account = null;
while (resultSet.next()){
account = new Account();
account.setId(resultSet.getString("id"));
account.setName(resultSet.getString("name"));
account.setBalance(resultSet.getInt("balance"));
accountList.add(account);
}
return accountList;
}
@Before
public void init() throws IOException, ClassNotFoundException, SQLException {
ClassLoader classLoader = Practice.class.getClassLoader();
properties = new Properties();
properties.load(classLoader.getResourceAsStream("pro.properties"));
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取数据库连接对象
connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password"));
//3.获取执行sql对象
statement = connection.createStatement();
}
@After
public void close() throws SQLException {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
resultSet Demo(无参考价值)
于 2022-03-28 01:01:26 首次发布