码迷,mamicode.com
首页 > 数据库 > 详细

mysql 模糊删除

时间:2015-12-10 16:45:29      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

可以先模糊查找,放到容器里面,在删除

 

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 模糊删除

标签:

原文地址:http://www.cnblogs.com/1130136248wlxk/p/5036259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!