Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dmitryporyvkin;
- import java.sql.*;
- public class Main {
- static final String URL = "jdbc:postgresql://localhost:5434/testdb";
- static final String JDBC_DRIVER = "org.postgresql.Driver";
- static final String USER = "postgres";
- static final String PASSWORD = "1234";
- public static void main(String[] args) throws SQLException , ClassNotFoundException{
- Connection connection = null;
- Statement statement = null;
- ResultSet resultSet;
- try{
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(URL , USER , PASSWORD);
- statement = connection.createStatement();
- statement.executeUpdate("DROP TABLE accounts");
- statement.executeUpdate("create table accounts("
- + "id SERIAL primary key , "
- + " username varchar(225) unique not null ," +
- " firstName varchar(225) not null , lastName varchar(225) not null) "
- );
- statement.executeUpdate("insert into accounts (username, firstName, lastName) " +
- "values ('firstuser','Ivan','Petrov')," +
- "('seconduser','Oleg','Tarasov')," +
- "('third','Nikita','Chernov');");
- statement.executeUpdate("update accounts set lastName = 'Sidorov' where username = 'seconduser' ");
- } catch (SQLException e) {
- System.out.println("Failed connection to database.");
- e.printStackTrace();
- }
- catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Driver not found.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement