更新数据

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;
    }
    
}

index.jsp

    
<% ResultSet rsall = db.getAllRs(); while(rsall.next()){ %> <% } %>
Id 用户名 密码
<%=rsall.getString("id") %> <%=rsall.getString("name") %> <%=rsall.getString("pwd") %>
Id:
用户名改为: 密码改为:

result.jsp

    
    <%
        String mess="";
        String id= request.getParameter("id");
        String updatename= request.getParameter("updatename");
        String updatepwd= request.getParameter("updatepwd");
        
    %>
    <%
            String a="id";
            String b=id;
            ResultSet rs = db.getPartRs(a, b);
            String sql="update u set name='"+updatename+"',pwd='"+updatepwd+"' where id="+b;
            int i=db.insert(sql);
            db.closed();
            if(i>0){
                mess="插入成功";
            }else{
                mess="插入失败";
            }
        %>
    
<%=mess %> 【返回主页】
更新数据_第1张图片
7.PNG
更新数据_第2张图片
8.PNG

你可能感兴趣的:(更新数据)