Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.util.Random;
  5. import java.lang.StringBuilder;
  6.  
  7. public class pvz {
  8. public static void main(String args[]) {
  9. Connection c = null;
  10. PreparedStatement smt = null;
  11. try {
  12. Class.forName("org.postgresql.Driver");
  13. c = DriverManager
  14. .getConnection("jdbc:postgresql://localhost:5432/naujasTEST",
  15. "postgres", "kurmis");
  16. c.setAutoCommit(false);
  17. System.out.println("Opened database successfully");
  18.  
  19. Random rand = new Random();
  20. StringBuilder sql = new StringBuilder("INSERT INTO COMPANY3 (ID,NAME,AGE,ADDRESS,SALARY) + VALUES;");
  21.  
  22. for (int j=0; j < 10; j++) {
  23. if (j == 1) sql.append(",");
  24. sql.append("(" + j + /* ID */
  25. ",'" + rand.nextInt() + "'" + /* NAME */
  26. ",'" + rand.nextInt() + "'" + /* AGE */
  27. ",'" + rand.nextInt() + "'" + /* ADDRESS */
  28. ",'" + rand.nextInt() + "'" + /* SALARY */
  29. ")");
  30. }
  31. //smt = c.createStatement();
  32. smt.executeUpdate();
  33. smt.close();
  34. c.commit();
  35. c.close();
  36. } catch (Exception e) {
  37. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  38. System.exit(0);
  39. }
  40. System.out.println("Records created successfully");
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement