Advertisement
Guest User

Untitled

a guest
May 8th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11.  
  12. public class DatabaseInsert {
  13.     Random rand = new Random();
  14.     public DatabaseInsert() throws SQLException{
  15.        
  16.         try {
  17.  
  18.         Class.forName("org.postgresql.Driver");
  19.  
  20.         } catch (ClassNotFoundException e) {
  21.             e.printStackTrace();
  22.  
  23.         }
  24.          
  25.         Connection connection = null;
  26.  
  27.         try {
  28.  
  29.         connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/cinemator", "postgres", "admin");
  30.  
  31.         } catch (SQLException e) {
  32.  
  33.         System.out.println("Connection Failed! Check output console");
  34.         e.printStackTrace();
  35.         return;
  36.  
  37.         }
  38.          
  39.         Statement stmt = connection.createStatement();
  40.         int i = 1, c;
  41.         int id;
  42.         String meno;
  43.         String heslo;
  44.         String rarity;
  45.         Scanner sc = new Scanner(System.in);
  46.         while (i < 30) {
  47.             System.out.print("meno ");
  48.             meno = sc.next();
  49.             System.out.print("priezvisok ");
  50.             String priezvisko = sc.next();
  51.             System.out.print("rola ");
  52.  
  53.             int rola = sc.nextInt();
  54.             String opis = "fasa";
  55.             System.out.print("pohlavie ");
  56.             String pohlavie = sc.next();
  57.             String sql = String.format("INSERT INTO artist VALUES ('%d','%s','%s','%s', '%s', '%d');", i, meno, priezvisko, opis, pohlavie, rola);
  58.             stmt.executeUpdate(sql);
  59.             i++;
  60.             }
  61.        
  62.         }
  63.        
  64.         public static void main(String[] args) throws SQLException {
  65.             new DatabaseInsert();
  66.         }
  67.    
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement