您需要爲要在其中保存這些電子郵件的數據庫添加其他驅動程序庫。
您可以在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它如何重複這個問題? –