📄 userbean.java
字号:
package userinfo;
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class UserBean {
private String id;
private String username;
private String password;
private String email;
private String phone;
private int logintime;
public static void main(String[] args) {
}
public UserBean() {
username = "";
password = "";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getLogintime() {
return logintime;
}
public void setLogintime(int logintime) {
this.logintime = logintime;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int login(String Id, String passwd) { // 验证用户是否已注册
int cnt = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection("jdbc:mysql://localhost/myphone?user=root&password=123456"); // 连接mysql数据库
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from userinfo where ID='"
+ Id + "' and PassWord='" + passwd + "'");
while (r.next()) {
cnt = 1;
// cnt=r.getInt(4);
s.close();
return cnt; // 如果数据库中已有该用户帐号,则返回真
}
s.close();
return cnt; // 否则返回假
} catch (Exception e) {
e.printStackTrace();
}
return cnt;
}
public int add() {
int cnt = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection("jdbc:mysql://localhost/myphone?user=root&password=123456"); // 连接mysql数据库
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from userinfo where ID='"
+ this.id + "'");
while (r.next()) {
cnt = 1;
s.close();
return cnt; // 如果数据库中已有该用户帐号,则返回真
}
r = s.executeQuery("select * from userinfo where Email='"
+ this.email + "'");
while (r.next()) {
cnt = 1;
s.close();
return cnt; // 如果数据库中已有该用户帐号,则返回真
}
SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd");//设置日期格式
String sql = "insert into userinfo values('" + this.id + "','"
+ this.username + "','" + this.password + "','"
+ this.phone + "','" + this.email + "','"+df.format(new Date())+"')";
System.out.println(sql);
s.executeUpdate(sql);
s.close();
return cnt; // 否则返回假
} catch (Exception e) {
e.printStackTrace();
}
return cnt;
}
public void select(String myid) {
int cnt = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection("jdbc:mysql://localhost/myphone?user=root&password=123456"); // 连接mysql数据库
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from userinfo where ID='"
+ myid + "'");
while (r.next()) {
this.id = myid;
this.username = r.getString(2);
this.password = r.getString(3);
this.phone = r.getString(4);
this.email = r.getString(5);
}
s.close();
// String sql = "insert into userinfo
// values('"+this.id+"','"+this.username+"','"+this.password+"','"+this.phone+"','"+this.email+"')";
// System.out.println(sql);
// s.executeUpdate(sql);
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int update() {
int cnt = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection("jdbc:mysql://localhost/myphone?user=root&password=123456"); // 连接mysql数据库
Statement s = c.createStatement();
String delsql = "delete from userinfo where ID='" + this.id + "'";
s.executeUpdate(delsql);
String sql = "insert into userinfo values('" + this.id + "','"
+ this.username + "','" + this.password + "','"
+ this.phone + "','" + this.email + "','+)";
// System.out.println(sql);
s.executeUpdate(sql);
s.close();
cnt = 1;
return cnt; // 否则返回假
} catch (Exception e) {
e.printStackTrace();
}
return cnt;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -