Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package databaza_pripojenie_testik;
- import java.util.Random;
- import java.sql.*;
- public class App {
- public static void main(String[] args) {
- String jdbcUrl = "jdbc:postgresql://localhost:5432/Letisko";
- String username = "postgres";
- String password = "postgres";
- Random srand = new Random();
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Step 1 - Load driver
- // Class.forName("org.postgresql.Driver"); // Class.forName() is not needed since JDBC 4.0
- // Step 2 - Open connection
- conn = DriverManager.getConnection(jdbcUrl, username, password);
- // Step 3 - Execute statement
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT * from customer");
- // Step 4 - Get result
- while (rs.next()) {
- System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
- }
- //VKLADANIE DO DATABAZY!
- String insertTableSQL = "INSERT INTO customer"
- + "(name, surname, passenger_ID) VALUES"
- + "(?,?,?)";
- PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL);
- preparedStatement.setString(1, "ABCD");
- preparedStatement.setString(2, "EFGH");
- preparedStatement.setString(3, "63968GDAG345");
- // execute insert SQL stetement
- preparedStatement .executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- // Step 5 Close connection
- if (stmt != null) {
- stmt.close();
- }
- if (rs != null) {
- rs.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement