在cn.Util包中添加需要用到的工具类,在数据库连接中已经完成了对DbUtil类的编写,然后我们继续完成下面项目用到的方法
1. 字符串判空操作的工具类——Stringutil.java
package cn.Util;
/**
* 字符串工具类
* @author 86185
*
*/
public class Stringutil {
/**
* 判断是否是空
* @param str
* @return
*/
public static boolean isEmply(String str) {
if(str==null || "".equals(str.trim())) {
return true;
}else {
return false;
}
}
/**
* 判断是否不是空
* @param str
* @return
*/
public static boolean notisEmoly(String str) {
if(str!=null && !"".equals(str)) {
return true;
}else {
return false;
}
}
}
2. 获取当前时间的工具包——DateUtill.java
package cn.Util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 用于得到当前日期
* @author 86185
*
*/
public class DateUtill {
public static String nowtime() {
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
String time=s.format(new Date());
return time;
}
/**
* 用于获取当前时间
* @return
*/
public static String nowdate() {
SimpleDateFormat s=new SimpleDateFormat("HH:mm:ss");
String time=s.format(new Date());
return time;
}
// public static void main(String[] args) {
// Date date=new Date();
// SimpleDateFormat s=new SimpleDateFormat("HH:mm:ss");
// String time=s.format(date);
// System.out.println(time);
// }
}
3. 用来做计数器的工具类——Count.java
package cn.Util;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
/**
* 计数器
* @author 86185
*
*/
public class Count {
public static String c1(boolean isfinlish) {
Properties pt=new Properties();
String filtpath="D:\\StudentDormSystem\\StudentDormSystem\\time.txt";
try{
FileReader fr=new FileReader(filtpath);
pt.load(fr);//加载文件
fr.close();
String code=pt.getProperty("code");//读出文件中的code的value
int num=Integer.parseInt(code);
if(isfinlish==true) {
num=++num;
}
String str=String.valueOf(num);
pt.setProperty("code",str);//把修改的值再写回文件中
FileWriter fw=new FileWriter(filtpath);
pt.store(fw,null);
fw.close();
return str;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public static String c2(boolean isfinlish) {
Properties pt=new Properties();
String filtpath="D:\\StudentDormSystem\\StudentDormSystem\\StuNum.txt";
try{
FileReader fr=new FileReader(filtpath);
pt.load(fr);//加载文件
fr.close();
String stunum=pt.getProperty("StuNum");
int num=Integer.parseInt(stunum);
if(isfinlish==true) {
num=++num;
}
String str=String.valueOf(num);
pt.setProperty("StuNum",str);
FileWriter fw=new FileWriter(filtpath);
pt.store(fw,null);
fw.close();
return str;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.print(Count.c1(true));
}
}
1. 学习新建一个JInternalFrame(内部窗口)文件,然后选择窗口左下角的design,然后就是开始按照要求完善窗口设计
1. 在cn.model包中编写与数据库对应的User类——User.java
用户表(UserInfo)
属性名 |
数据类型 |
约束 |
用户编号(UserID) |
Char(5) |
primary key not null |
用户姓名(UserName) |
varchar(20) |
not null |
密码(UserPwd) |
varchar(20) |
not null |
性别(UserSex) |
varchar(10) |
not null |
级别(UserLevel) |
varchar(20) |
not null |
地址(UserAddress) |
varchar(50) |
not null |
联系电话(UserPhone) |
Char(20) |
not null |
package cn.model;
/**
* 用户封装类
* @author 86185
*
*/
public class User {
private String UserId;
private String UserName;
private String UserPwd;
private String UserSex;
private String UserLevel;
private String UserAddress;
private String UserPhone;
public User(String userName, String userPwd) {
super();
UserName = userName;
UserPwd = userPwd;
}
public User(String userId, String userName, String userSex, String userLevel, String userAddress, String userPhone) {
super();
UserId = userId;
UserName = userName;
UserSex = userSex;
UserLevel = userLevel;
UserAddress = userAddress;
UserPhone = userPhone;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String userPwd, String userSex, String userAddress, String userPhone) {
super();
UserName = userName;
UserPwd = userPwd;
UserSex = userSex;
UserAddress = userAddress;
UserPhone = userPhone;
}
public User(String userId, String userName, String userPwd, String userSex, String userLevel, String userAddress,
String userPhone) {
super();
UserId = userId;
UserName = userName;
UserPwd = userPwd;
UserSex = userSex;
UserLevel = userLevel;
UserAddress = userAddress;
UserPhone = userPhone;
}
public User(String userName, String userPwd,String userlevel) {
super();
UserName = userName;
UserPwd = userPwd;
UserLevel=userlevel;
}
public String getUserId() {
return UserId;
}
public void setUserId(String userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPwd() {
return UserPwd;
}
public void setUserPwd(String userPwd) {
UserPwd = userPwd;
}
public String getUserSex() {
return UserSex;
}
public void setUserSex(String userSex) {
UserSex = userSex;
}
public String getUserLevel() {
return UserLevel;
}
public void setUserLevel(String userLevel) {
UserLevel = userLevel;
}
public String getUserAddress() {
return UserAddress;
}
public void setUserAddress(String userAddress) {
UserAddress = userAddress;
}
public String getUserPhone() {
return UserPhone;
}
public void setUserPhone(String userPhone) {
UserPhone = userPhone;
}
}
2. 在cn.Dao包中创建一个UserDao类用来编写关于用户的SQL语句——UserDao.java
package cn.Dao;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import cn.model.User;
/**
* UserDao类用于查询数据库信息
* @author 86185
*
*/
public class UserDao {
/**
* 登陆功能
* sql语句验证登陆
* @param con
* @param user
* @return
* @throws Exception
*/
public User login(Connection con,User user)throws Exception{
User resultuser=null;//使其一开始为空
String sql="select * from userinfo where username=? and userpwd=? and userlevel=?";
PreparedStatement pps=con.prepareStatement(sql);
pps.setString(1, user.getUserName());
pps.setString(2, user.getUserPwd());
pps.setString(3, user.getUserLevel());
ResultSet rs=pps.executeQuery();
if(rs.next()) {//用于判断是否通过验证
resultuser=new User();
resultuser.setUserId(rs.getString("userid"));
resultuser.setUserName(rs.getString("username"));
resultuser.setUserPwd(rs.getString("userpwd"));
resultuser.setUserSex(rs.getString("usersex"));
resultuser.setUserLevel(rs.getString("userlevel"));
resultuser.setUserPhone(rs.getString("userphone"));
resultuser.setUserAddress(rs.getString("useraddress"));
}
return resultuser;
}
}
3. 创建一个LoginFrm类继承JFrame,然后用下载的Swing Designer插件来设计主页面
4.为按钮添加事件,之前已经添加过了,选择actionPerformed(按钮单击方法),然后它就会跳转到源码中,就可以添加方法,具体编写方法
/**
* 重置按钮事件监听方法
* @param evt
*/
private void resetActionperformed(ActionEvent evt) {
resetxt();
}
/**
* 登陆按钮事件监听方法
* @param e
*/
private void LoginActionPreformed(ActionEvent evt) {
username=this.usernametxt.getText();
userpwd=this.userpwdtxt.getText();
if(Stringutil.isEmply(username)) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}else if(Stringutil.isEmply(userpwd)) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
Connection con=null;
try {
con=dbutil.getCon();
if(this.User_1rb.isSelected()) {
User resultuser=userdao.login(con, new User(username,userpwd,"操作员"));
if(resultuser!=null) {
this.userlevel="操作员";
this.dispose();
new MainFrm().setVisible(true);
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
resetxt();
}
}else if(this.User_2rb.isSelected()) {
User resultuser=userdao.login(con, new User(username,userpwd,"管理员"));
if(resultuser==null) {
User S_resultuser=userdao.login(con, new User(username,userpwd,"超级管理员"));
if(S_resultuser!=null) {
this.userlevel="超级管理员";
this.dispose();
new M_MainFrm().setVisible(true);
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
resetxt();
}
}
else if(resultuser!=null) {
this.userlevel="管理员";
this.dispose();
new M_MainFrm().setVisible(true);
}
}
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "无法连接服务器!");
}finally {
try {
dbutil.closecon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 重置文本框方法
*/
public void resetxt() {
usernametxt.setText("");
userpwdtxt.setText("");
}