当前位置:   article > 正文

Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)_android studio 的userdao

android studio 的userdao

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import entity.User;

public class UserDao {

public boolean login(String name,String password){

String sql = “select * from users where name = ? and password = ?”;

Connection con = JDBCUtils.getConn();

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,name);

pst.setString(2,password);

if(pst.executeQuery().next()){

return true;

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return false;

}

public boolean register(User user){

String sql = “insert into users(name,username,password,age,phone) values (?,?,?,?,?)”;

Connection con = JDBCUtils.getConn();

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,user.getName());

pst.setString(2,user.getUsername());

pst.setString(3,user.getPassword());

pst.setInt(4,user.getAge());

pst.setString(5,user.getPhone());

int value = pst.executeUpdate();

if(value>0){

return true;

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return false;

}

public User findUser(String name){

String sql = “select * from users where name = ?”;

Connection con = JDBCUtils.getConn();

User user = null;

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,name);

ResultSet rs = pst.executeQuery();

while (rs.next()){

int id = rs.getInt(1);

String namedb = rs.getString(2);

String username = rs.getString(3);

String passworddb = rs.getString(4);

int age = rs.getInt(5);

String phone = rs.getString(6);

user = new User(id,namedb,username,passworddb,age,phone);

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return user;

}

}

5、创建对应的LoginServlet

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import dao.UserDao;

public cla

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/646911
推荐阅读
相关标签
  

闽ICP备14008679号