Java JDBC MySQL Connection removeById(int Id)

Here you can find the source of removeById(int Id)

Description

remove By Id

License

Apache License

Declaration

public static void removeById(int Id) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    private static Statement statement = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;
    private static Connection conn = null;

    public static void removeById(int Id) {
        try {// www . ja va  2s . c  o  m
            //establish connection
            conn = DriverManager.getConnection("jdbc:mysql://localhost/sbs_db?" + "user=root&password=");

            preparedStatement = conn.prepareStatement("DELETE FROM sbs_userdata WHERE Id=" + Id);
            // Result set get the result of the SQL query
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Error occurs while delete entry from database!");
            e.printStackTrace();
        } finally {
            close();
        }
    }

    private static void close() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            System.err.println("Error occurs while close DB connection");
        }
    }
}

Related

  1. openConnection()
  2. putData(int tID, String toolID)
  3. query(String sql)
  4. queryAll(String sql, List params)
  5. readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)
  6. searchForItemId(String item)
  7. searchServerPropertySplitMinutes()
  8. tablesOk(String url, String user, String password)

  9. HOME | Copyright © www.java2s.com 2016