1.做个人事管理系统
第一个类主类
package wode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* Created by ttc on 18-1-23.
*/
public class JDBCDemoFinal {
public static int delectEmpById(int id) throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("DELETE FROM EMP WHERE sid=?");
preparedStatement.setInt(1,id);
int rows=preparedStatement.executeUpdate();
JDBCUtils.close(preparedStatement,connection);
return rows;
}
public static int update(Emp emp) throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("update emp set sname=?,passwordnum=?,email=?,permission=?,birthday=?WHERE sid=?");
preparedStatement.setString(1,emp.getSname());
preparedStatement.setInt(2,emp.getPasswordnum());
preparedStatement.setString(3,emp.getEmail());
preparedStatement.setString(4,emp.getPermission());
Date date=emp.getBirthday();
long time=date.getTime();
java.sql.Timestamp sqlDate = new java.sql.Timestamp(time);
preparedStatement.setTimestamp(5,sqlDate);
preparedStatement.setInt(6,emp.getSid());
int rows=preparedStatement.executeUpdate();
JDBCUtils.close(preparedStatement,connection);
return rows;
}
public static int AddEmp(Emp emp) throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("INSERT INTO emp (sname,passwordnum,email,permission,birthday)VALUES (?,?,?,?,?)");
preparedStatement.setString(1,emp.getSname());
preparedStatement.setInt(2,emp.getPasswordnum());
preparedStatement.setString(3,emp.getEmail());
preparedStatement.setString(4,emp.getPermission());
Date date=emp.getBirthday();
long time2=date.getTime();
java.sql.Timestamp sqlDate = new java.sql.Timestamp(time2);
preparedStatement.setTimestamp(5,sqlDate);
int rows=preparedStatement.executeUpdate();
JDBCUtils.close(preparedStatement,connection);
return rows;
}
public static Emp getEmpById(int id) throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp WHERE sid=?");
preparedStatement.setInt(1,id);
ResultSet resultSet=preparedStatement.executeQuery();
Emp emp=null;
if (resultSet.next()==true)
{
int sid=resultSet.getInt("sid");
String name=resultSet.getString("sname");
int password=resultSet.getInt("passwordnum");
String email=resultSet.getString("email");
String permission=resultSet.getString("permission");
Date date=resultSet.getTimestamp("birthday");
emp=new Emp();
emp.setSid(sid);
emp.setSname(name);
emp.setPasswordnum(password);
emp.setEmail(email);
emp.setPermission(permission);
emp.setBirthday(date);
}
JDBCUtils.close(resultSet,preparedStatement,connection);
return emp;
}
public static Emp getEmpBySname(String yname) throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp WHERE sname like ?");
preparedStatement.setString(1,"%"+yname+"%");
ResultSet resultSet=preparedStatement.executeQuery();
Emp emp=null;
if (resultSet.next()==true)
{
int sid=resultSet.getInt("sid");
String name=resultSet.getString("sname");
int password=resultSet.getInt("passwordnum");
String email=resultSet.getString("email");
String permission=resultSet.getString("permission");
Date date=resultSet.getTimestamp("birthday");
emp=new Emp();
emp.setSid(sid);
emp.setSname(name);
emp.setPasswordnum(password);
emp.setEmail(email);
emp.setPermission(permission);
emp.setBirthday(date);
}
JDBCUtils.close(resultSet,preparedStatement,connection);
return emp;
}
public static List getAllEmpBySname() throws SQLException {
Connection connection=JDBCUtils.getConn();
PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp");
ListempList=new ArrayList<>();
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()==true)
{
int sid=resultSet.getInt("sid");
String name=resultSet.getString("sname");
int password=resultSet.getInt("passwordnum");
String email=resultSet.getString("email");
String permission=resultSet.getString("permission");
Date date=resultSet.getTimestamp("birthday");
Emp emp=new Emp();
emp.setSid(sid);
emp.setSname(name);
emp.setPasswordnum(password);
emp.setPermission(permission);
emp.setEmail(email);
emp.setBirthday(date);
empList.add(emp);
}
JDBCUtils.close(resultSet,preparedStatement,connection);
return empList;
}
public static void main(String[] args) throws SQLException, ParseException {
// Emp emp=new Emp();
// emp.setSname("zhangsan");
// emp.setPasswordnum(123);
// emp.setEmail("[email protected]");
// emp.setPermission("管理员");
// String date1="1980-05-13";
// SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
// Date date=simpleDateFormat.parse(date1);
// emp.setBirthday(date);
// AddEmp(emp);
System.out.println("欢迎使用neusoft的用户管理系统");
System.out.println("=============================");
System.out.println("用户登录-----------------1");
System.out.println("用户注册-----------------2");
System.out.println("退出程序-----------------3");
Scanner scanner=new Scanner(System.in);
while (true)
{
int command=scanner.nextInt();
if (command==2)
{
System.out.println("用户注册界面");
System.out.println("=============================");
System.out.println("请输入你的用户名");
String name=scanner.next();
System.out.println("请输入你的密码");
int num=scanner.nextInt();
System.out.println("请输入你的邮箱");
String email=scanner.next();
System.out.println("请输入你的生日");
String birthday=scanner.next();
Emp emp=new Emp();
emp.setSname(name);
emp.setPasswordnum(num);
emp.setEmail(email);
emp.setPermission("普通用户");
String s=birthday;
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date date=simpleDateFormat.parse(s);
emp.setBirthday(date);
Listlist=getAllEmpBySname();
Listnamelist=new ArrayList<>();
for (Emp emp2:list)
{
namelist.add(emp2.getSname());
}
if (namelist.contains(name))
{
System.out.println("注册失败,已有用户名");
}
else
{
AddEmp(emp);
System.out.println("注册成功");
}
}
if (command==1)
{
System.out.println("请输入你的用户名");
String name=scanner.next();
System.out.println("请输入你的密码");
int password=scanner.nextInt();
Listlist=getAllEmpBySname();
for (Emp emp:list)
{
if (emp.getSname().equals(name)&&emp.getPasswordnum()==password)
{
System.out.println("登录成功。。。");
System.out.println("=============================");
System.out.println("欢迎登录主窗体");
System.out.println(name+"您好:"+"\t"+"您的权限是:"+emp.getPermission());
System.out.println("=============================");
if (emp.getPermission().equals("普通用户"))
{
while (true)
{
System.out.println("修改自己的信息------------------1");
System.out.println("查询自己的信息------------------2");
System.out.println("程序退出------------------------3");
int command1=scanner.nextInt();
if (command1==3)
{
break;
}
if (command1==1)
{
Emp emp1=getEmpBySname(name);
if (emp1!=null)
{
System.out.println("您现在的信息是");
System.out.println(emp1);
System.out.println("=============================");
System.out.println("请输入你要修改的姓名");
String newname=scanner.next();
System.out.println("请输入你要修改的密码");
int newpassword=scanner.nextInt();
System.out.println("请输入要修改的邮箱");
String newemail=scanner.next();
Emp emp2=new Emp();
emp2.setSname(newname);
emp2.setPasswordnum(newpassword);
emp2.setEmail(newemail);
emp2.setPermission(emp.getPermission());
Date date=emp.getBirthday();
emp2.setBirthday(date);
emp2.setSid(emp.getSid());
update(emp2);
System.out.println("修改成功");
}
else
{
System.out.println("不存在");
}
}
if (command1==2)
{
Emp emp1=getEmpBySname(name);
System.out.println("您现在的信息是");
System.out.println(emp1);
}
}
}
if (emp.getPermission().equals("管理员"))
{
while (true)
{
System.out.println("添加用户-----------------1");
System.out.println("删除用户-----------------2");
System.out.println("修改用户-----------------3");
System.out.println("查询用户-----------------4");
System.out.println("程序退出-----------------5");
int command2=scanner.nextInt();
if (command2==5)
{
break;
}
if (command2==4)
{
System.out.println("查询全部用户----------1");
System.out.println("根据ID查询用户--------2");
System.out.println("根据姓名查询用户------3");
System.out.println("请输入要做的操作");
int command3=scanner.nextInt();
if (command3==2)
{
System.out.println("请输入要查询的ID");
int id=scanner.nextInt();
Emp emp3=getEmpById(id);
System.out.println(emp3);
}
if (command3==3)
{
System.out.println("请输入要查询的用户名(支持模糊查询)");
String names=scanner.next();
Emp emp4=getEmpBySname(names);
System.out.println(emp4);
}
if (command3==1)
{
Listlist1=getAllEmpBySname();
for (Emp emp1:list1)
{
System.out.println(emp1);
}
}
}
if (command2==2)
{
System.out.println("请输入要删除的用户的ID号码");
int num=scanner.nextInt();
delectEmpById(num);
System.out.println("删除用户成功");
}
if (command2==1)
{
System.out.println("请输入你的用户名");
String name3=scanner.next();
System.out.println("请输入你的密码");
int num=scanner.nextInt();
System.out.println("请输入你的邮箱");
String email=scanner.next();
System.out.println("请输入你的生日");
String birthday=scanner.next();
Emp emp1=new Emp();
emp1.setSname(name3);
emp1.setPasswordnum(num);
emp1.setEmail(email);
emp1.setPermission("普通用户");
String s=birthday;
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date date=simpleDateFormat.parse(s);
emp1.setBirthday(date);
AddEmp(emp1);
System.out.println("添加用户成功");
}
if (command2==3)
{
System.out.println("请输入要修改的用户的ID号码");
int id=scanner.nextInt();
System.out.println("请输入你要修改的姓名");
String newname=scanner.next();
System.out.println("请输入你要修改的密码");
int newpassword=scanner.nextInt();
System.out.println("请输入要修改的邮箱");
String newemail=scanner.next();
System.out.println("请输入要修改的用户的权限(管路员/普通用户)");
String word=scanner.next();
Emp emp1=new Emp();
emp1.setSid(id);
emp1.setSname(newname);
emp1.setPasswordnum(newpassword);
emp1.setEmail(newemail);
Emp emp2=getEmpById(id);
emp1.setBirthday(emp2.getBirthday());
emp1.setPermission(word);
update(emp1);
System.out.println("修改成功");
}
}
}
}
}
}
}
}
}
第二个副类
package wode;
import java.util.Date;
/**
* Created by ttc on 18-1-23.
*/
public class Emp {
private int sid;
private String sname;
private int passwordnum ;
private String email;
private String permission;
private Date birthday;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getPasswordnum() {
return passwordnum;
}
public void setPasswordnum(int passwordnum) {
this.passwordnum = passwordnum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Emp{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", passwordnum=" + passwordnum +
", email='" + email + '\'' +
", permission='" + permission + '\'' +
", birthday=" + birthday +
'}';
}
}
第三个类
package wode;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Created by ttc on 18-1-23.
*/
public class JDBCUtils {
private static Connection conn=null;
private static String url;
private static String username;
private static String password;
static{
InputStream in =JDBCUtils .class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName(properties.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
try {
conn= DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
}
private JDBCUtils(){}
public static Connection getConn()
{
try {
conn = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Statement statement, Connection connection)
{
if(statement != null)
{
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null)
{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection)
{
if(resultSet != null)
{
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null)
{
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null)
{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}