JavaBean专题

删除数据

2018-08-19  本文已影响0人  神坛下的我

Util.java

import java.sql.*;

public class Util {
    private Connection conn;
    private Statement stm;
    private ResultSet rs;
    private String classname="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/test";
    public Util(){
        try {
            Class.forName(classname);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /*获取连接*/
    public Connection getConn() {
        try {
            conn=DriverManager.getConnection(url,"root","123456");
        } catch (SQLException e) {
            e.printStackTrace(System.err);
            conn=null;
        }
        return conn;
    }
    /*关闭连接*/
    public void closed(){
        
            try {
                if (rs!=null) {
                    rs.close();
                }
                if (stm!=null) {
                    stm.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    
    
    public void setConn(Connection conn) {
        this.conn = conn;
    }
    /*执行语句*/
    public Statement getStm() {
        
        try {
            conn=getConn();
            stm=conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stm;
    }
    public Statement getStmed(){
        
        try {
            conn=getConn();
            stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stm;
    }
    public ResultSet getAllRs(){
        String sql="select * from u";
        
        try {
            stm=getStmed();
            rs=stm.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public ResultSet getPartRs(String subsql,String subsqlvalue){
        if(subsql==null){
            subsql="";
        }
        if(subsqlvalue==null){
            subsqlvalue="";
        }
        String sql= "select * from u where "+subsql+" ="+subsqlvalue;
        
        try {
            stm=getStmed();
            rs=stm.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    /*插入数据*/
    public int insert(String sql){
        int num=-1;
        if(sql==null) sql="";
        
        try {
            stm=getStmed();
            num=stm.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            num=-1;
        }
        return num;
    }
    /*更新数据*/
    public int update(String sql){
        int num=-1;
        if(sql==null) sql="";
        
        try {
            stm=getStmed();
            num=stm.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            num=-1;
        }
        return num;
    }
    /*删除数据*/
    public int delete(String sql){
        int num=-1;
        if(sql==null) sql="";
        
        try {
            stm=getStmed();
            num=stm.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            num=-1;
        }
        return num;
    }
    
}

index.jsp

<body>
    <form action="result.jsp" method="post">
        <table>
            <tr bgcolor="lightgrey" height="25">
                <td align="center">Id</td>
                <td align="center">用户名</td>
                <td align="center">密码</td>
            </tr>
            <jsp:useBean id="db" class="com.count.Util" scope="page"></jsp:useBean>
            <%
                ResultSet rsall = db.getAllRs();
                while(rsall.next()){
            %>
                <tr>
                    <td align="center"><%=rsall.getString("id") %></td>
                    <td align="center"><%=rsall.getString("name") %></td>
                    <td align="center"><%=rsall.getString("pwd") %></td>
                </tr>
            <%
                }
            %>
            <tr bgcolor="lightgrey">
                <td colspan="1" align="center">Id:</td>
                <td colspan="3" align="center">
                    <input type="text" name="id">
                </td>
                <td align="center" colspan="1">
                    <input type="submit" name="searchsubmit" value="删除该用户"/>
                </td>
            </tr>
        </table>
    </form>

result.jsp

    <jsp:useBean id="db" class="com.count.Util" scope="page"></jsp:useBean>
    <%
        String mess="";
        String id= request.getParameter("id");
    %>
    <%
            String a="id";
            String sql="delete from u where id="+id;
            int i=db.delete(sql);
            db.closed();
            if(i>0){
                mess="插入成功";
            }else{
                mess="插入失败";
            }
        %>
    <table>
        
        <tr><td align="center"><%=mess %></td>
            <td align="left"><a href="index.jsp">【返回主页】</a></td>
        </tr>
    </table>
9.PNG 10.PNG
上一篇下一篇

猜你喜欢

热点阅读