mercredi 26 avril 2017

Java inserting random letters into database table [duplicate]

This question already has an answer here:

I have a code that generates random numbers and inserts them into a database, but i want to generate random letters as well. Is there a way to so with some minor changes to the code below or do i need to change it completely?

public static void insertToTable(Connection c, String tableName, int numberOfRows) throws UnsupportedEncodingException, SQLException {
        Random rand = new Random();
        StringBuilder sql =
                new StringBuilder("INSERT INTO " + tableName + " (NR,PIRMAS,ANTRAS,TRECIAS,KETVIRTAS,PENKTAS,SESTAS,SEPTINTAS,ASTUNTAS,DEVINTAS) VALUES ");

        for (int j = 0; j < numberOfRows; j++) {
            if (j != 0) sql.append(",");
            sql.append("(" + j +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ",'" + String.valueOf(rand.nextInt()) + "'" +
                    ")");


        } 




Aucun commentaire:

Enregistrer un commentaire