您需要为要在其中保存这些电子邮件的数据库添加其他驱动程序库。
您可以在maven central的数据库中找到jdbc驱动程序。
对于mysql数据库一般的代码可以是这样的:
ArrayList<String> objectsToStore = new ArrayList<>();
Random rnd = new Random();
for (int i = 1; i <= 5; i++) {
objectsToStore.add("username" + rnd.nextInt() + "@gmail.com");
}
try {
//1) this used to load mysql jdbc driver into memory
Class.forName("com.mysql.jdbc.Driver");
//2) create connection to running mysql instance
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName?useSSL=false", "username", "password");
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
for (String objectToStore : objectsToStore) {
// this insert will work assuming you have table user_data with email field
statement.executeUpdate("INSERT INTO USER_DATA (email) VALUES ('" + objectToStore +"')");
}
//commit transaction
connection.commit();
statement.close();
connection.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
SQL在数据库中创建一个表:
create table User_data(
email varchar(255)
);
要保存它在哪里?在文件,数组,数据库等? –
存储它在哪里?一个数据库?一个txt文件..? –
@ScaryWombat它如何重复这个问题? –