所有编码方式都已经统一为GBK了(包括数据表),还是乱码啊,无限纠结ingprotected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session=request.getSession(false);
        String id=session.getAttribute("id").toString();
        String Address=request.getParameter("address");
        Information info=new Information();
        info.setInfo(id, "Address", Address);//更新数据表中Address属性的值
        request.setAttribute("success", "修改信息成功!");
        PrintWriter out = response.getWriter();
        try {
            //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ModifyInfo</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ModifyInfo at " + request.getContextPath () + "</h1>");
            out.println("<br>"+Address);
            out.print("<br>"+info.getInfo(id, "Address"));//获取数据表中Address属性的值,就是这里中文显示乱码
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }    }

解决方案 »

  1.   

    package Student;
    import java.sql.*;
    import Database.DatabaseConnection;
    public class Information {
        private String info;
        //private boolean flag;
        public Information(){
        }
        public void setInfo(String id, String property, String value) {
            DatabaseConnection c=new DatabaseConnection();
            Connection con=null;
            Statement smt=null;
            con=c.getMysqlConnection("together", "root", "123");
            String sql="UPDATE studentinfo SET "+property+"= '"+value+"' WHERE StudentID='"+id+"'";
            try{
                smt=con.createStatement();
                smt.executeUpdate(sql);
                con.close();
                smt.close();
            }catch( SQLException e){
                System.out.println("=SQLException:"+e.getMessage());
            }
        }
    }
      

  2.   


    package Database;
    import java.sql.*;
    public class DatabaseConnection {
        public Connection getMysqlConnection(String DatabaseName,String user,String password){
                Connection con=null;
                String driver="com.mysql.jdbc.Driver";
                String url="jdbc:mysql://127.0.0.1:3306/"+DatabaseName+"?user="+user+"&password="+password;
                try{
                    Class.forName(driver);
                }catch(ClassNotFoundException e){
                    System.out.println("Class.forName:"+e.getMessage());
                }
                try{
                    con=DriverManager.getConnection(url);
                }catch(SQLException e){
                    System.out.println("=SQLException:"+e.getMessage());
                }
                return con;
            }
    }