Advertisement
Guest User

Untitled

a guest
Feb 28th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package databaza_pripojenie_testik;
  2. import java.util.Random;
  3. import java.sql.*;
  4.  
  5. public class App {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. String jdbcUrl = "jdbc:postgresql://localhost:5432/Letisko";
  10. String username = "postgres";
  11. String password = "postgres";
  12. Random srand = new Random();
  13. Connection conn = null;
  14. Statement stmt = null;
  15. ResultSet rs = null;
  16.  
  17. try {
  18. // Step 1 - Load driver
  19. // Class.forName("org.postgresql.Driver"); // Class.forName() is not needed since JDBC 4.0
  20.  
  21. // Step 2 - Open connection
  22. conn = DriverManager.getConnection(jdbcUrl, username, password);
  23.  
  24. // Step 3 - Execute statement
  25. stmt = conn.createStatement();
  26. rs = stmt.executeQuery("SELECT * from customer");
  27.  
  28. // Step 4 - Get result
  29. while (rs.next()) {
  30. System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
  31. }
  32. //VKLADANIE DO DATABAZY!
  33. String insertTableSQL = "INSERT INTO customer"
  34. + "(name, surname, passenger_ID) VALUES"
  35. + "(?,?,?)";
  36.  
  37. PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL);
  38. preparedStatement.setString(1, "ABCD");
  39. preparedStatement.setString(2, "EFGH");
  40. preparedStatement.setString(3, "63968GDAG345");
  41. // execute insert SQL stetement
  42. preparedStatement .executeUpdate();
  43.  
  44.  
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. } finally {
  48. try {
  49.  
  50. // Step 5 Close connection
  51. if (stmt != null) {
  52. stmt.close();
  53. }
  54. if (rs != null) {
  55. rs.close();
  56. }
  57. if (conn != null) {
  58. conn.close();
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement