Neusoft entity
package com.neusoft.entity;
public class Neusoft {
private Integer id;
private String name;
private String password;
private String email;
private String qx;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQx() {
return qx;
}
public void setQx(String qx) {
this.qx = qx;
}
@Override
public String toString() {
return "id为" + id + ", 账号为" + name + ",密码为" + password + ", 邮箱为" + email + ", 权限为" + qx;
}
public Neusoft(Integer id, String name, String password, String email, String qx) {
super();
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.qx = qx;
}
public Neusoft() {
super();
}
}
dao
NeusoftDaoimpi
package com.neusoft.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.entity.Neusoft;
public class NeusoftDaoImpi {
public List getAll() throws SQLException{
String sql="select * from Neusoft order by id";
Connection cn = jdbcConnection.getconnection();
ResultSet rs = jdbcConnection.executeQuery(cn, sql, null);
List list=new ArrayList<>();
Neusoft neu=null;
while(rs.next()) {
int id=rs.getInt("id");
String name =rs.getString("name");
String password=rs.getString("password");
String email=rs.getString("email");
String qx =rs.getString("qx");
neu= new Neusoft(id, name, password, email, qx);
list.add(neu);
}
rs.close();
jdbcConnection.closeConnection(cn);
return list;
}
public Neusoft getbyid(int id) throws SQLException {
String sql="select * from neusoft where id=?";
Connection cn = jdbcConnection.getconnection();
ResultSet rs = jdbcConnection.executeQuery(cn, sql,new Object[]{id});
Neusoft neu=null;
while(rs.next()) {
int sid=rs.getInt("id");
String name =rs.getString("name");
String password=rs.getString("password");
String email=rs.getString("email");
String qx =rs.getString("qx");
neu= new Neusoft(sid, name, password, email, qx);
}
rs.close();
jdbcConnection.closeConnection(cn);
return neu;
}
public int inserti(Neusoft neu) throws SQLException {
String sql="insert into neusoft values(null,?,?,?,default)";
int n = jdbcConnection.executeUpdate(sql, new Object[]{neu.getName(),neu.getPassword(),neu.getEmail()});
return n;
}
public int del(int id) throws SQLException {
String sql="DELETE FROM neusoft WHERE id=?;";
int n = jdbcConnection.executeUpdate(sql, new Object[]{id});
return n;
}
public int up(Neusoft neu) throws SQLException {
String sql="UPDATE neusoft SET NAME=?,password=?,email=?,qx=? WHERE id=? ";
int n = jdbcConnection.executeUpdate(sql, new Object[]{neu.getName(),neu.getPassword(),neu.getEmail(),neu.getQx(),neu.getId(),});
return n;
}
public boolean login(String name,String password) throws SQLException {
String sql="select * from neusoft where name=? and password = ?";
Connection cn = jdbcConnection.getconnection();
System.out.println("xxx");
ResultSet rs = jdbcConnection.executeQuery(cn, sql,new Object[]{name,password});
Neusoft neu=null;
boolean x=false;
while(rs.next()) {
int sid=rs.getInt("id");
String name1 =rs.getString("name");
String password1=rs.getString("password");
if(name1!=("")|password1!=("")){
System.out.println(name1+password);
x=true;
}else{
x=false;
}
}
//neu= new Neusoft(sid, name, password, email, qx);
return x;
}
public Neusoft loginy(String name,String password) throws SQLException {
String sql="select * from neusoft where name=? and password = ?";
Connection cn = jdbcConnection.getconnection();
ResultSet rs = jdbcConnection.executeQuery(cn, sql,new Object[]{name,password});
Neusoft neu=null;
while(rs.next()) {
int sid=rs.getInt("id");
String name1 =rs.getString("name");
String password1=rs.getString("password");
String email=rs.getString("email");
String qx =rs.getString("qx");
neu= new Neusoft(sid, name1, password1, email, qx);
}
rs.close();
jdbcConnection.closeConnection(cn);
return neu;
}
public Neusoft getbyusername(String na) throws SQLException {
String sql="select * from neusoft where name like ?";
Connection cn = jdbcConnection.getconnection();
ResultSet rs = jdbcConnection.executeQuery(cn, sql,new Object[]{na});
Neusoft neu=null;
while(rs.next()) {
int sid=rs.getInt("id");
String name =rs.getString("name");
String password=rs.getString("password");
String email=rs.getString("email");
String qx =rs.getString("qx");
neu= new Neusoft(sid, name, password, email, qx);
}
rs.close();
jdbcConnection.closeConnection(cn);
return neu;
}
public Neusoft getqx(int id) throws SQLException {
String sql="select * from neusoft where id=?";
Connection cn = jdbcConnection.getconnection();
ResultSet rs = jdbcConnection.executeQuery(cn, sql,new Object[]{id});
Neusoft neu=null;
while(rs.next()) {
int sid=rs.getInt("id");
String name =rs.getString("name");
String password=rs.getString("password");
String email=rs.getString("email");
String qx =rs.getString("qx");
neu= new Neusoft(sid, name, password, email, qx);
}
rs.close();
jdbcConnection.closeConnection(cn);
return neu;
}
}
jdbc连接
package com.neusoft.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class jdbcConnection {
private static String driverClassName = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test2";
private static String username = "root";
private static String password = "root";
static {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}// 获得链接
public static Connection getconnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, username, password);
//System.out.println("连接成功");
return connection;
}
// 改链接
public static void closeConnection(Connection connection) throws SQLException {
connection.close();
}
// 增删改 条件 sql语句 以及问号个数
public static int executeUpdate(String sql, Object[] params) throws SQLException {
Connection connection = getconnection();
PreparedStatement st = connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
}
int n = st.executeUpdate();
st.close();
connection.close();
return n;
}
public static ResultSet executeQuery(Connection connection,String sql, Object[] params) throws SQLException {
PreparedStatement st = connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
}
ResultSet rs=st.executeQuery();
return rs;
}
}
主界面
package com.neuspft.ui;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import com.neusoft.dao.NeusoftDaoImpi;
import com.neusoft.entity.Neusoft;
public class jz {
public static void main(String[] args) throws SQLException {
String name;
String password;
String email;
Neusoft dly=null;
NeusoftDaoImpi neus=new NeusoftDaoImpi();
boolean b = true;
while(b){
System.out.println("欢迎使用neusoft系统");
System.out.println("================");
System.out.println("登陆系统=========1");
System.out.println("注册系统=========2");
System.out.println("退出系统=========3");
Scanner s=null;
int x=0;
try {
s = new Scanner(System.in);
x = s.nextInt();
} catch (Exception e) {
System.out.println("输入的值不是数字");
}
switch(x){
case 1:
System.out.print("请输入账号");
name=s.next();
System.out.print("请输入密码");
password=s.next();
boolean p=neus.login(name, password);
if(p==true){
System.out.println("登陆成功");
dly=neus.loginy(name,password);
System.out.println("欢迎登陆主窗体");
System.out.println("尊敬的"+dly.getName()+"你好\t\t您的权限为"+dly.getQx());
if(dly.getQx().equals("管理员")){
System.out.println("管理员");
boolean gl=true;
while(gl){
System.out.println("=============");
System.out.println("添加用户======1");
System.out.println("删除用户======2");
System.out.println("修改用户======3");
System.out.println("查询用户======4");
System.out.println("退出程序======5");
int g=s.nextInt();
switch(g){
case 1:
System.out.print("请输入账号");
name=s.next();
System.out.print("请输入密码");
password=s.next();
System.out.println("请输入邮箱");
email=s.next();
Neusoft neu= new Neusoft(null, name, password, email, null);
int z=neus.inserti(neu);
if(z==1){
System.out.println("添加用户成功,请牢记相关信息");
}else{
System.out.println("发生未知错误,注册失败");
}
continue;
case 2:
System.out.println("输入需要删除的id");
int sc=s.nextInt();
int c=neus.del(sc);
if(c==1){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
break;
case 3:
System.out.println("请输入要修改id");
int u=s.nextInt();
Neusoft nue2=neus.getqx(u);
if(nue2.getQx().equals("管理员")){
System.out.println("若想修改管理员的权限请联系数据库管理员");
break;
}
System.out.println("请输入要修改账户");
String u1=s.next();
System.out.println("请输入要修改密码");
String u2=s.next();
System.out.println("请输入要修改邮箱");
String u3=s.next();
System.out.println("请输入要修改权限");
String u4=s.next();
if(u4.equals("管理员")){
System.out.println("管理员具有修改用户信息的权限确定保存修改?");
System.out.println("是======1");
System.out.println("否======2");
int pd=s.nextInt();
switch(pd){
case 1:
System.out.println("正在保存修改");
Neusoft neu1 = new Neusoft(u, u1, u2, u3, u4);
int up=neus.up(neu1);
if(up==1){
System.out.println("更改已被保存");
}
else{
System.out.println("发生未知错误请联系管理员");
}
break;
case 2:
System.out.println("已舍弃该修改");
break;
}
}
break;
case 4:
System.out.println("请输入查询的方式");
System.out.println("查询所有用户==1");
System.out.println("根据id查询==2");
System.out.println("根据姓名查询==3");
int cx=s.nextInt();
switch(cx){
case 1:
List li=neus.getAll();
for(Neusoft N:li){
System.out.println(N);
}
break;
case 2:
System.out.println("请输入需要查询的id");
int x1=s.nextInt();
Neusoft n=neus.getbyid(x1);
System.out.println("id为"+n.getId()+"账号为"+n.getName()+"密码为"+n.getPassword()+"邮箱为"+n.getEmail()+"权限为"+n.getQx());
break;
case 3:
System.out.println("请输入要查询的账户(支持模糊查询)");
String na=s.next();
Neusoft n1=neus.getbyusername("%"+na+"%");
System.out.println("id为"+n1.getId()+"账号为"+n1.getName()+"密码为"+n1.getPassword()+"邮箱为"+n1.getEmail()+"权限为"+n1.getQx());
break;
}
case 5:
gl=false;
}
}
}else{
Neusoft neu3= neus.getbyusername(name);
System.out.println("普通用户");
System.out.println("=============");
System.out.println("修改个人信息======1");
System.out.println("查询个人信息======2");
System.out.println("退出程序======3");
int g=s.nextInt();
switch(g){
case 1:
System.out.println("请输入修改后的账户名");
String u1=s.next();
System.out.println("请输入要修改密码");
String u2=s.next();
System.out.println("请输入要修改邮箱");
String u3=s.next();
Neusoft neu4 = new Neusoft(null, u1, u2, u3, null);
int jg=neus.up(neu4);
if(jg==1){
System.out.println("修改个人信息成功");
}//else{
//System.out.println("发生未知错误请联系管理员");
// }
break;
case 2:
Neusoft n2=neus.getbyid(neu3.getId());
System.out.println("id为"+n2.getId()+"账号为"+n2.getName()+"密码为"+n2.getPassword()+"邮箱为"+n2.getEmail()+"权限为"+n2.getQx());
break;
case 3:
break;
}
}
break;
}else{
System.out.println("未搜索到相关信息,请注册后尝试");
continue;
}
case 2:
System.out.print("请输入账号");
name=s.next();
System.out.print("请输入密码");
password=s.next();
System.out.println("请输入邮箱");
email=s.next();
Neusoft neu= new Neusoft(null, name, password, email, null);
int z=neus.inserti(neu);
if(z==1){
System.out.println("注册成功,请牢记账号密码");
}else{
System.out.println("发生未知错误,注册失败");
}
continue;
case 3:
System.out.println("正在退出系统");
break;
default:
System.out.println("输入有误");
break;
}
b=false;
}
}
}