Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SET SQL_SAFE_UPDATES = 0;
- public class DatabaseWork {
- // подключение к бд
- private static final String url = "jdbc:mysql://localhost:3306/players?autoReconnect=true&useSSL=false";
- private static final String user = "user";
- private static final String password = "password";
- // переменные подключения
- private static Connection con = null;
- private static Statement stmt = null;
- private static ResultSet rs = null;
- // строка запроса
- private String query;
- public void banIp(String ipaddress, String reason) {
- query = "INSERT INTO `bannedip` (`ip`, `reason`) VALUES ('" + ipaddress + "', '" + reason + "');";
- try {
- con = DriverManager.getConnection(url, user, password);
- // getting Statement object to execute query
- stmt = con.createStatement();
- stmt.executeUpdate(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- finally {
- try {
- con.close();
- } catch (SQLException se) {
- se.printStackTrace(); }
- try {
- stmt.close();
- } catch (SQLException se) {
- se.printStackTrace(); }
- }
- }
- DatabaseWork db = new DatabaseWork();
- db.banIp("hjg996k", "reason");
- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'hjg996k' for key 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement