zoukankan      html  css  js  c++  java
  • 团队冲刺第二天--校园百晓生(STDeverything)

    摘要:今天实现了安卓端使用http将用户注册信息存入云服务器mysql数据库。

    遇到问题:安卓端注册请求http成功返回200,但是刷新数据库发现里面并没有数据。

    原因:云服务器数据库相对于云服务器是本地,url地址应为URL="jdbc:mysql://localhost:3306,而我写入了服务器地址号(低级错误找了半天)

    解决方法:URL="jdbc:mysql://localhost:3306

    代码部分:

    package Entity;
    
    public class Users {
    
        public Users() {
            // TODO Auto-generated constructor stub
        }
        
        public String getUserid() {
            return userid;
        }
    
        public void setUserid(String userid) {
            this.userid = userid;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        public String getDate() {
            return date;
        }
    
        public void setDate(String date) {
            this.date = date;
        }
    
        private String userid;
        private String username;
        private String pwd;
        private String date;
    
    }
    User
    package Servlet;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import DBUtil.DBUtil;
    import Entity.Users;
    
    /**
     * Servlet implementation class login_servlet
     */
    @WebServlet("/login_servlet")
    public class login_servlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public login_servlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
         */
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=UTF-8");
            String method=request.getParameter("method");
            System.out.println("执行service");
            if(method.equals("register"))
                registerUser(request, response);
            else if(method.equals("login"))
                checkLogin(request, response);
        }
        
        private void registerUser(HttpServletRequest request, HttpServletResponse response) throws IOException {   //用户注册
            Users users=new Users();
            users.setUsername(request.getParameter("name"));
            users.setPwd(request.getParameter("pwd"));
            DBUtil.registerUser(users);
        }
        
        private void checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {  //用户登录
            String name=request.getParameter("name");
            String pwd=request.getParameter("pwd");
            response.getWriter().write(DBUtil.checklogin(name, pwd));
        }
        
        
    
    }
    servlet
    public class DBUtil {
    
        private static String model="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        private static String URL="jdbc:mysql://localhost:3306/stdeverything?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
        private static String username="root";
        private static String password="password";
        public static Connection getConnection() {
            Connection connection=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                connection=DriverManager.getConnection(URL, username, password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        public DBUtil() {
            // TODO Auto-generated constructor stub
        }
        
        private static void closeDB(Connection connection,Statement statement,ResultSet resultSet) {  //关闭连接
            if(connection!=null)
                try {
                    connection.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            if(statement!=null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if(resultSet!=null)
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        
        public static String getDateString(boolean type) {       //获取当前时间
            Date date=new Date();
            SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMddHHmmssSSS");
            return type?sdf1.format(date):sdf2.format(date);
        }
        
        public static String getIdentifier() {    //生成编码
            String str="";
            for(int i=0;i<10;i++)
                str+=model.charAt((int)(Math.random()*100)%62);
            return str+getDateString(false);
        }
        
        public static void registerUser(Users users) {   //用户注册
            String sql="insert into user values('"+getIdentifier()+"','"+users.getUsername()+"','"+users.getPwd()+"','"+getDateString(true)+"','')";
            Connection connection=getConnection();
            try {
                Statement statement=connection.createStatement();
                statement.executeUpdate(sql);
                closeDB(connection, statement, null);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("用户注册出错");
            }
        }
        
        public static String checklogin(String name,String pwd) {    //检查登录
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("status", "0");
            String sql="select * from user where name='"+name+"'";
            String sql_id=null,sql_pwd=null;
            Connection connection=getConnection();
            ResultSet rs=null;
            try {
                Statement statement=connection.createStatement();
                rs=statement.executeQuery(sql);
                if(rs.next()) {
                    sql_id=rs.getString("id");
                    sql_pwd=rs.getString("pwd");
                    jsonObject.put("id", sql_id);
                    jsonObject.put("name", "yes");
                }else {
                    jsonObject.put("id", "");
                    jsonObject.put("name", "no");
                    jsonObject.put("msg", "用户名不存在");
                }
                closeDB(connection, statement, rs);
                if(jsonObject.get("name").equals("yes")) {
                    if(pwd.equals(sql_pwd)) {
                        jsonObject.put("msg", "登陆成功");
                        jsonObject.put("status", "1");
                    }else {
                        jsonObject.put("msg", "密码不正确");                
                    }
                }
                
                return jsonObject.toString();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("登陆时出错");
            }
            return null;
        }
    DBUtil

    打包放入服务器然后再android端请求服务器就ok了!

    数据库:

    明天尝试添加第三方登录(首先考虑腾讯)

  • 相关阅读:
    jquery 选择器总结
    jQuery 添加元素和删除元素
    jQuery 操作
    jquery 事件
    jQuery对象与DOM对象
    jquery 在页面中三种写法
    CSS3制作立体导航
    开发常用技巧之css字体编码
    c语言快速入门3
    c语言快速入门2
  • 原文地址:https://www.cnblogs.com/dd110343/p/12728386.html
Copyright © 2011-2022 走看看