mysql 模糊刪除

來源:互聯網
上載者:User

標籤:

可以先模糊尋找,放到容器裡面,在刪除

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Newsdbo {
    private Connection conn = null;
    PreparedStatement statement = null;

    // connect to MySQL
    public void connSQL() {
        String urle = "jdbc:mysql://192.168.1.100:3306/state?useUnicode=true&characterEncoding=utf-8";// port:3306
                                                                                                        // database:testdb
        String username = "root";// user
        String password = "";// password

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }// 載入驅動,串連資料庫
            conn = DriverManager.getConnection(urle, username, password);
        }
        // 捕獲載入驅動程式異常
        catch (ClassNotFoundException cnfex) {
            System.err.println("裝載 JDBC/ODBC 驅動程式失敗。");
            cnfex.printStackTrace();
        }
        // 捕獲串連資料庫異常
        catch (SQLException sqlex) {
            System.err.println("無法串連資料庫");
            sqlex.printStackTrace();
        }
    }

    // disconnect to MySQL
    public void deconnSQL() {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            System.out.println("關閉資料庫問題 :");
            e.printStackTrace();
        }
    }

    // execute selection language
    public ResultSet selectSQL(String sql) {
        ResultSet rs = null;
        try {
            statement = conn.prepareStatement(sql);
            rs = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    // execute insertion language
    public boolean insertSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("插入資料庫時出錯:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入時出錯:");
            e.printStackTrace();
        }
        return false;
    }

    public boolean insert(String sql, Object[] params) {
        if (sql != null && !sql.equals("")) {
            PreparedStatement pstm = null;
            if (params == null)
                params = new Object[0];
            if (conn != null) {
                try {
                    pstm = (PreparedStatement) conn.prepareStatement(sql,
                            ResultSet.TYPE_SCROLL_INSENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
                    for (int i = 0; i < params.length; i++) {
                        pstm.setObject(i + 1, params[i]);
                    }
                    pstm.execute();
                    return true;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return false;
    }

    // execute delete language
    public boolean deleteSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("插入資料庫時出錯:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入時出錯:");
            e.printStackTrace();
        }
        return false;
    }

    // execute update language
    public boolean updateSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("插入資料庫時出錯:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入時出錯:");
            e.printStackTrace();
        }
        return false;
    }

    // show data in ju_users
    public List<String> layoutStyle2(ResultSet rs) {
        List<String> topics = new ArrayList<String>();
        String desc;

        try {
            while (rs.next()) {
                desc = new String();
                System.out.println(rs.getString(1));
                desc = rs.getString(1);

                topics.add(desc);
            }
        } catch (SQLException e) {
            System.out.println("顯示時資料庫出錯。");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("顯示出錯。");
            e.printStackTrace();
        }
        return topics;
    }

    public static void main(String args[]) {
        System.out.println("sfs");
        List<String> descid = new ArrayList<String>();
        Newsdbo newsdbo = new Newsdbo();
        newsdbo.connSQL();
        String select = "select *from recorder where  descid like ‘%kaidi%‘";
        descid = newsdbo.layoutStyle2(newsdbo.selectSQL(select));
        // String delete = "delete from recorder";
        // newsdbo.deleteSQL(delete);
        for (int i = 0; i < descid.size(); ++i) {
            String delete = "delete from recorder where descid=‘"
                    + descid.get(i) + "‘";
            newsdbo.deleteSQL(delete);
        }
        newsdbo.deconnSQL();
    }
}

mysql 模糊刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.