выводятся лишние данные в sql
я написал запрос в sql из intellij idea и когда я вывожу их они выводятся несколько раз мой код в DBWorker:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBWorker {
private final String URL = "jdbc:postgresql://localhost:5432/postgres";
private final String LOGIN = "root";
private final String PASSWORD = "password";
public DBWorker() {
try {
Class.forName("org.postgresql.Driver");
System.out.println("Driver loading success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public List<Client> getAllClients() {
Connection connection = null;
java.sql.Statement statement = null;
List<Client> clients = new ArrayList<>();
try {
connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT * FROM test");
while (resultSet.next()){
String first_name = resultSet.getString("first_name");
String last_name = resultSet.getString("last_name");
Client client = new Client(first_name, last_name);
clients.add(client);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try{
connection.close();
statement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
return clients;
}
}
код в client:
public class Client {
private String first_name;
private String last_name;
public Client(String first_name, String last_name){
this.first_name = first_name;
this.last_name = last_name;
}
public String getFirst_name(){ return first_name; }
public String getLast_name(){ return last_name; }
}
Main:
import java.util.List;
public class Main {
public static void main(String[] args) {
DBWorker worker = new DBWorker();
List<Client> clients = worker.getAllClients();
for (Client client : clients) {
System.out.println(client.getFirst_name() + " "+ client.getLast_name());
}
}
}
код в Statement:
import java.sql.* ;
public class Statement{
private static final String URL = "jdbc:postgresql://localhost:5432/postgres";
private static final String LOGIN = "root";
private static final String PASSWORD = "password";
public static void main(String[] args) {
registerDriver();
Connection connection = null;
java.sql.Statement statement = null;
new Statement();{
System.out.println("statement");
}
try {
connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
statement = connection.createStatement();
statement.addBatch("INSERT INTO test (last_name, first_name) VALUES('Voldarin', 'Alex')");
statement.execute("DELeTE From test WHERE first_name = 'alexey'");
/*int res = statement.executeUpdate("UPDATE test SET first_name = 'Alexey' WHERE first_name = 'Alex' ");
System.out.println(res);*/
statement.addBatch("INSERT INTO test(last_name, first_name) VALUES('Shonarev', 'Sergey')");
statement.addBatch("INSERT INTO test(last_name, first_name) VALUES('Mironova', 'Sofia')");
statement.addBatch("INSERT INTO test(last_name, first_name) VALUES('Smirnova', 'Margarita')");
statement.executeBatch();
boolean closed = statement.isClosed();
boolean closed2 = connection.isClosed();
System.out.println(closed);
System.out.println(closed2);
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void registerDriver() {
try {
Class.forName("org.postgresql.Driver");
System.out.println("Driver loading success");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Вывод:
Driver loading success!
Voldarin alexey
Shonarev Sergey
Sofia Mironova
Smirnova Margarita
Voldarin alexey
Shonarev Sergey
Sofia Mironova
Smirnova Margarita
Voldarin Alex
Shonarev Sergey
Sofia Mironova
Smirnova Margarita
Alex Voldarin
Sergey Shonarev
Sofia Mironova
Margarita Smirnova
Alex Voldarin
Sergey Shonarev
Sofia Mironova
Margarita Smirnova
Alex Voldarin
Sergey Shonarev
Sofia Mironova
Margarita Smirnova
Ответы (1 шт):
Проблема в том что вы в запросе используете *
Синтаксис
SELECT DISTINCT column1, column2, ...
FROM table_name;
То есть мы должны указать перечень колонок, по которым будут браться уникальные значения. Использовав * вы выбираете все поля, в том числе и поле id, у которого значение для каждой записи уникально. Поэтому у вас и выводятся абсолютно все строки из таблицы.