📄 userdao.java
字号:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.bean.User;
import com.util.DBUtil;
public class UserDAO {
/**
* 根据用户名查询用户的信息
* */
public User findUserByName(String name)
{
Connection conn = null;
conn = DBUtil.getJDBCConnection();
User user = new User();
String sql = "select username,nickname,sex,birthyear,birthmonth,birthday,e_mail,mobile,signature,usergrade,userscore from bbsuser where username=?";
try {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
String username =rs.getString(1);
String nickname =rs.getString(2);//会员昵称
String sex =rs.getString(3);//会员性别
String birthyear =rs.getString(4);//出生年
String birthmonth =rs.getString(5);//出生月
String birthday =rs.getString(6);//出生日
String email =rs.getString(7);//电子邮件地址
String mobile =rs.getString(8);//手机号码
String signature =rs.getString(9);//会员签名档
String usergrade =rs.getString(10);//用户等级
String userscore =rs.getString(11);//用户积分
System.out.println(username+"aaaaaaaaa");
user = new User(username,nickname,sex,birthyear,birthmonth,birthday,email,mobile,signature,usergrade,userscore);
}
System.out.println(user.getUsername());
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally
{
DBUtil.closeConnection(conn);
}
return user;
}
/*
* 用户登录
* */
public boolean findUser(User user)
{
boolean flag = false;
Connection conn = null;
conn = DBUtil.getJDBCConnection();
String sql = "select username,password from bbsuser where username = ? and password = ?";
try {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
ResultSet rs = stmt.executeQuery();
if(rs.next())
{
flag = true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
flag = false;
e.printStackTrace();
}
finally
{
DBUtil.closeConnection(conn);
}
return flag;
}
/*
* 注册新用户
* */
public boolean addUsers(User user)
{
boolean flag = false;
Connection conn = null;
conn = DBUtil.getJDBCConnection();
String sql = "insert into bbsuser(username,[password],nickname,sex,birthyear,birthmonth,birthday,e_mail,mobile,signature,usergrade,userscore) values(?,?,?,?,?,?,?,?,?,?,'普通用户','0')";
try {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getNickname());
stmt.setString(4, user.getSex());
stmt.setString(5, user.getBirthyear());
stmt.setString(6, user.getBirthmonth());
stmt.setString(7, user.getBirthday());
stmt.setString(8, user.getEmail());
stmt.setString(9, user.getMobile());
stmt.setString(10, user.getSignature());
int res = stmt.executeUpdate();
System.out.println(res);
if(res==1)
{
flag = true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
//flag = false;
e.printStackTrace();
}
finally
{
DBUtil.closeConnection(conn);
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -