900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > JSP连接数据库实现注册登录

JSP连接数据库实现注册登录

时间:2023-07-01 14:18:22

相关推荐

JSP连接数据库实现注册登录

1.使用单例来连接数据库 //数据库的连接使用单例来连接一次即可,执行完sql语句后如果没有结束就不能关流 不然后边就不能再去执行sql语句

public class JDBCUtils1 {

// 连接数据库的参数

private String driver= "com.mysql.jdbc.Driver";

private String url = "jdbc:mysql://localhost:3306/xinzhi666?useUnicode=true&characterEncoding=utf-8";

private String username = "root";

private String password = "root";

private static Connection conn;

private JDBCUtils1(){

try {

Class.forName(driver);

conn = DriverManager.getConnection(url, username, password);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

}

public static Connection getConnection(){

if(conn!=null){

new JDBCUtils1();

}

return conn;

}

public void relase(ResultSet resultSet, Statement statement,Connection connection){

try {

if(resultSet!=null){

resultSet.close();

}

if(statement!=null){

statement.close();

}

if(connection!=null){

connection.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

2.写注册的接口

public interface IUserInfoDao {

/**

* 注册 数据库中添加用户

* @param ui 用户实体类

* @return

*/

int addUser(UserInfo ui);

}

3.写注册的实现类

public class UserInfoDaoImpl implements IUserInfoDao {

@Override

public int addUser(UserInfo ui) {

int count = -1;

PreparedStatement pstat = null;

try {

String sql = "insert into user (username,password,age,phone) values (?,?,?,?)";

pstat = JDBCUtils.getConnection().prepareStatement(sql);

pstat.setString(1, ui.getUsername());

pstat.setString(2, ui.getPassword());

pstat.setInt(3, ui.getAge());

pstat.setString(4, ui.getPhone());

count = pstat.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally{

JDBCUtils.relase(null, pstat, null);

}

return count;

}

2.注册的界面

<form action="regService.jsp" method="post">

用户名:<input type="text" name="username" /><br>

密码:<input type="text" name="password" /><br>

年龄:<input type="text" name="age" /><br>

手机号:<input type="text" name="phone" /><br>

<input type="submit" value="注册">

</form>

3.注册的后台逻辑

<%

request.setCharacterEncoding("UTF-8");

String username = request.getParameter("username");

String password = request.getParameter("password");

String age = request.getParameter("age");

String phone = request.getParameter("phone");

UserInfo ui = new UserInfo();

ui.setAge(Integer.parseInt(age));

ui.setUsername(username);

ui.setPassword(password);

ui.setPhone(phone);

UserInfoDaoImpl uidi = new UserInfoDaoImpl();

int count = uidi.addUser(ui);

if(count > 0){

response.sendRedirect("login.jsp");

}else{

request.setAttribute("info", "注册失败");

request.getRequestDispatcher("register.jsp").forward(request, response);

}

%>

登录功能

1.写登录的接口

/**** @param username 用户名* @param password 密码* @return 查询的数据*/User queryByUsernameAndPassword(String username,String password);

根据输入的账号密码来进入数据库查询

2.写登录的实现类

@Overridepublic User queryByUsernameAndPassword(String username, String password) {// 通过工具类获取连接对象 connectionConnection connection = BaseDao.getConnection();// 插入的sql语句String sql = "select id,username,password,age from user where username=? and password=?";PreparedStatement statement = null;ResultSet resultSet = null;User user = null;try {//获取statement对象statement = connection.prepareStatement(sql);statement.setString(1,username);statement.setString(2,password);resultSet = statement.executeQuery();if (resultSet.next()){int id = resultSet.getInt(1);String uname = resultSet.getString(2);String pw = resultSet.getString(3);int age = resultSet.getInt(4);user = new User(id, uname, pw, age);}} catch (SQLException throwables) {throwables.printStackTrace();}return user;}

3.写登录的界面

<form action="loginService.jsp" method="post">

用户名:<input type="text" name="username" />

密码:<input type="text" name="password" />

<input type="submit" value="登陆">

</form>

4.写登录的后台逻辑

<%

//设置页面请求编码格式

request.setCharacterEncoding("UTF-8");

//获取页面传过来的用户名和密码

String username = request.getParameter("username");

String password = request.getParameter("password");

// 登录逻辑

UserInfoDaoImpl uidi = new UserInfoDaoImpl();

User user = uidi.login(username, password);

// 获取年龄

ShowInfoDaoImpl showInfoDao = new ShowInfoDaoImpl();

Set<Integer> ages = showInfoDao.getAge();

if(user != null){

// 登陆成功

ShowInfoDaoImpl sidi = new ShowInfoDaoImpl();

List<User> uis = sidi.getUserList(-1);

// 域对象中设置属性(request域对象的特点)

request.setAttribute("uis", uis);

request.setAttribute("ages",ages );

request.getRequestDispatcher("show.jsp").forward(request, response);

}else{

// 登陆失败

request.setAttribute("info", "登陆失败");

request.getRequestDispatcher("login.jsp").forward(request, response);

}

%>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。