📄 user.java
字号:
package beans;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.BaseSQL;
public class User {
private String userName = "";
private String realName = "";
private String password = "";
private int type = 0;
//get set 方法
public String deleteUser(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery("select userName from user where userName = '"+userName+"'");
if(rs.next()){
b.statement.executeUpdate("delete from user where userName = '"+userName+"'");
return null;
}else{
return "不存在该用户!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
}
//实例user
public String addNewUser(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery("select userName from user where userName = '"+userName+"'");
if(rs.next()){
return "用户已经存在!";
}else{
b.connection.setAutoCommit(false);
b.statement.executeUpdate("insert into user values ('"
+userName+"','"+realName+"','"+password+"',"+type+")");
b.connection.commit();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String changePassword(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
b.connection.setAutoCommit(false);
b.statement.executeUpdate(
"update user set password = '"+password+"' where userName = '"+userName + "'");
b.connection.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String getData(String userName, String password){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select * from user where username = '"+userName+"'and password = '"+password+"'");
if(rs.next()){
this.userName = rs.getString("userName");
this.realName = rs.getString("realName");
this.password = rs.getString("password");
this.type = rs.getInt("type");
}else{
return "用户不存在或密码错误!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String getWaiterData(String userName){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select * from user where username = '"+userName+"'");
if(rs.next()){
this.userName = rs.getString("userName");
this.realName = rs.getString("realName");
this.password = rs.getString("password");
this.type = rs.getInt("type");
}else{
return "用户不存在!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean equals(String compare){
if(this.userName.equals(compare)){
return true;
}else{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -