There is such a code

public boolean addVisitor (Visitor visitor){ boolean isAdded = false; try (PreparedStatement statement = connection.prepareStatement ("INSERT INTO visitors (address,rm_host,rm_port,method,user_agent) values (?,?,?,?,?)")){ statement.setString(1,visitor.getRemoteAddress()); statement.setString(2,visitor.getRemoteHost()); statement.setString(3,visitor.getRemotePort()); statement.setString(4,visitor.getRequestMethod()); statement.setString(5,visitor.getUserAgent()); statement.execute(); if(statement.getWarnings()==null) isAdded =true; } catch (SQLException e){ e.printStackTrace(); } return isAdded; } 

He is trying to add a record to the database, if the record in the database already exists, then there will be an exception in the statement that the record exists and the value is not added. In the tomketa log com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '127.0.0.1' for key 'address_UNIQUE' How correctly did I implement the way to add unique entries to the database? All this can be implemented on java by creating a sheet, adding and checking the presence of records in it, and then entering them into the database.

    1 answer 1

    It is better to first check if there is such a visitor, if not, then add if there is something to report that already exists.

    • It is possible instead of the select section to add the appropriate processing SQLExceprion . - Igor Kudryashov