0

here this is my random mail generator code, I would like to save that random mails, how can I do that?

public class stupit {

    public static void main(String[] args) {
        Random randomGenerator = new Random();

        for (int i=1; i<=5; i++) {
            int randomInt = randomGenerator.nextInt(1000);
            System.out.println("username"+randomInt+"@gmail.com");
        }
    }
}

Output is:

 username394@gmail.com
 username429@gmail.com
 username70@gmail.com
 username419@gmail.com
 username744@gmail.com

how to save these out put like a = username394@gmail.co , b=username429@gmail.com .....

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71

2 Answers2

1

You need additional driver library for the database in which you are going to save these emails.
You can find jdbc driver for your database in maven central.

For mysql database general code can look like:

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 x : objectsToStore) {
            // this insert will work assuming you have table user_data with email field
            statement.executeUpdate("INSERT INTO USER_DATA (email) VALUES ('" + x +"')");
        }
        //commit transaction
        connection.commit();
        statement.close();
        connection.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

SQL for creating a table in database:

create table User_data(
  email varchar(255)
);
maiorovi
  • 81
  • 1
  • 6
  • 1
    _Exception in thread "main" java.lang.Error: Unresolved compilation problem: Duplicate local variable objectsToStore at new_test.main(new_test.java:29)_ –  Jul 20 '17 at 07:18
  • '(String objectsToStore: objectsToStore )' this line error –  Jul 20 '17 at 07:21
  • you duplicate variable name in a cycle. Should be '(String objectToStore : objectsToStore) – maiorovi Jul 21 '17 at 07:03
-1

If you want to save them...thn declare a string[ ] arrsy in outside of the loop....then try to store the mail addresses inside the string using nested loop...

And you can also store them in a text file using FileIlnputStrem class....ina txt file...

Sohan Kumar
  • 85
  • 1
  • 9