Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package databaza_generovanie;
- import com.github.javafaker.Faker;
- import java.sql.*;
- import java.util.Random;
- public class Runner {
- public static void main(String[] args) throws SQLException {
- Connection con = null;
- PreparedStatement st = null;
- Faker faker = new Faker();
- try {
- Class.forName("org.postgresql.Driver");
- con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbs_projekt", "postgres", "heslo");
- con.setAutoCommit(false);
- System.out.println("Otvorena databaza.");
- //vkladanie
- // String sql = "INSERT INTO lekar (ID, meno, prax) " + "VALUES (3, 'Ema Kumanova', 4);";
- // st.executeUpdate(sql);
- //
- // st = con.createStatement();
- // sql = "INSERT INTO lekar (ID, meno, prax) " + "VALUES (4, 'Peter Volen', 5);";
- // st.executeUpdate(sql);
- String name;
- Random rand_cislo_prax = new Random();
- int n;
- int poradove_cislo;
- st=con.createprepareStatement("INSERT INTO lekar (ID, meno, prax) VALUES (default, ?, ?)");
- for (int i=0; i<6; i++) {
- name = faker.name().fullName();
- n = rand_cislo_prax.nextInt(50) + 2;
- poradove_cislo = i+4;
- st.setString(1,name);
- st.setInt(2, n);
- st.executeUpdate(sql);
- }
- // ResultSet rs = st.executeQuery( "SELECT * FROM lekar;" );
- // while ( rs.next() ) {
- // int id = rs.getInt("ID");
- // String meno = rs.getString("meno");
- // int prax = rs.getInt("prax");
- //
- // System.out.println( "ID = " + id );
- // System.out.println( "MENO = " + meno );
- // System.out.println( "PRAX = " + prax );
- //
- // System.out.println();
- // }
- // rs.close();
- st.close();
- con.commit();
- con.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (con != null) {
- con.close();
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement