java用户管理类的代码,java用户管理类的代码有哪些

用java编写一个用户信息管理器

import java.util.ArrayList;

专注于为中小企业提供成都网站设计、成都网站制作、外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业浔阳免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

import java.util.List;

import java.util.Scanner;

/**

* @author young

*

*/

class User{

private String no;

private String name;

private int age;

private String add;

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAdd() {

return add;

}

public void setAdd(String add) {

this.add = add;

}

}

public class Test {

// 增加

public static ArrayListUser addUser(){

ListUser list = new ArrayListUser();

User u = new User();

Scanner sc = new Scanner(System.in);

System.out.print("输入 no: ");

u.setNo(sc.nextLine());

System.out.print("输入name: ");

u.setName(sc.nextLine());

System.out.print("输入add: ");

u.setAdd(sc.nextLine());

System.out.print("输入age: ");

u.setAge(sc.nextInt());

list.add(u);

return (ArrayListUser) list;

}

// 修改

public static void updateUser(String name, ListUser list){

Scanner sc = new Scanner(System.in);

boolean isExist = false;

for(int i = 0; i  list.size(); i++){

if(name.equals(list.get(i).getName())){

isExist = true;

System.out.print("输入新 no: ");

list.get(i).setNo(sc.nextLine());

System.out.print("输入新name: ");

list.get(i).setName(sc.nextLine());

System.out.print("输入新add: ");

list.get(i).setAdd(sc.nextLine());

System.out.print("输入新age: ");

list.get(i).setAge(sc.nextInt());

}

}

if(isExist == false){

System.out.println("不存在该用户");

}

}

// 查找

public static void findUser(String name, ListUser list){

boolean isExist = false;

for(int i = 0; i  list.size(); i++){

if(name.equals(list.get(i).getName())){

isExist = true;

System.out.println("用户编号: " + list.get(i).getNo()

+ ". 姓名: " + list.get(i).getName()

+ ". 地址: " + list.get(i).getAdd()

+ ". 年龄: " + list.get(i).getAge());

}

}

if(isExist == false){

System.out.println("不存在该用户");

}

}

// 删除

public static void delUser(String name, ListUser list) {

boolean isExist = false;

for (int i = 0; i  list.size(); i++) {

if (name.equals(list.get(i).getName())) {

isExist = true;

list.remove(i);

System.out.println("该用户已删除");

}

}

if (isExist == false) {

System.out.println("不存在该用户");

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

ListUser list = new ArrayListUser();

int i = 0;

while(true){

System.out.println("输入你的选择: 1.增加\t2.查找\t3.修改\t4.删除\t0.退出");

i = sc.nextInt();

switch (i) {

case 1:

list = addUser();

break;

case 2:

System.out.print("输入要查找的用户名: ");

String name = sc.next();

findUser(name, list);

break;

case 3:

System.out.print("输入要修改的用户名: ");

String n = sc.next();

updateUser(n, list);

break;

case 4:

System.out.print("输入要删除的用户名: ");

String na = sc.next();

delUser(na, list);

break;

case 0:

System.out.println("程序结束!");

System.exit(0);

break;

}

}

}

}

JAVA高手看看一下,怎么下提示?

展开全部

类似于下面那样连接数据库:

package com.bjsxt.drp.util;

import java.sql.*;

public class DB {

public static Connection getConn() {

Connection conn = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/drp?user=rootpassword=root");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static Statement getStatement(Connection conn) {

Statement stmt = null;

try {

if(conn != null) {

stmt = conn.createStatement();

}

} catch (SQLException e) {

e.printStackTrace();

}

return stmt;

}

public static ResultSet getResultSet(Statement stmt, String sql) {

ResultSet rs = null;

try {

if(stmt != null) {

rs = stmt.executeQuery(sql);

}

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}

public static void closeConn(Connection conn) {

try {

if(conn != null) {

conn.close();

conn = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

public static void closeStmt(Statement stmt) {

try {

if(stmt != null) {

stmt.close();

stmt = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

public static void closeRs(ResultSet rs) {

try {

if(rs != null) {

rs.close();

rs = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

下面是个用户Bean:

package com.bjsxt.drp.sysmgr;

import java.util.Date;

/**

* 用户实体类

* @author Administrator

*

*/

public class User {

//用户代码

private String userId;

//用户名称

private String userName;

//密码

private String password;

//联系电话

private String contactTel;

//email

private String email;

//创建日期

private Date createDate;

public String getContactTel() {

return contactTel;

}

public void setContactTel(String contactTel) {

this.contactTel = contactTel;

}

public Date getCreateDate() {

return createDate;

}

public void setCreateDate(Date createDate) {

this.createDate = createDate;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

下面是一个用户管理类来对用户进行增删改查:

package com.bjsxt.drp.sysmgr;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Timestamp;

import java.util.ArrayList;

import java.util.List;

import com.bjsxt.drp.util.DB;

import com.bjsxt.drp.util.PageModel;

/**

* 对用户完成增删改查的类

* @author Administrator

*

*/

public class UserManager {

// private static UserManager instance;

//

// private UserManager() {

//

// }

//

// public static synchronized UserManager getInstance() {

// if (instance == null) {

// instance = new UserManager();

// }

// return instance;

// }

private static UserManager instance = new UserManager();

private UserManager() {

}

public static UserManager getInstance() {

return instance;

}

/**

* 增加用户

* @param user user用户对象

*/

public void addUser(User user) {

String sql ="insert into t_user(user_id, user_name, password, contact_tel, email, create_date) " +

"values(?, ?, ?, ?, ?, ?)";

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = DB.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, user.getUserId());

pstmt.setString(2, user.getUserName());

pstmt.setString(3, user.getPassword());

pstmt.setString(4, user.getContactTel());

pstmt.setString(5, user.getEmail());

pstmt.setTimestamp(6, new Timestamp(user.getCreateDate().getTime()));

pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally {

DB.closeStmt(pstmt);

DB.closeConn(conn);

}

}

/**

* 根据用户代码查询

* @param userId 用户代码

* @return user对象

*/

public User findUserById(String userId) {

String sql = "select * from t_user where user_id=?";

System.out.println("findUserById()==sql=" + sql);

User user = null;

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

try {

conn = DB.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, userId);

rs = pstmt.executeQuery();

if (rs.next()) {

user = new User();

user.setUserId(rs.getString("user_id"));

user.setUserName(rs.getString("user_name"));

user.setPassword(rs.getString("password"));

user.setContactTel(rs.getString("contact_tel"));

user.setEmail(rs.getString("email"));

user.setCreateDate(rs.getTimestamp("create_date"));

}

}catch(SQLException e) {

e.printStackTrace();

}finally {

DB.closeRs(rs);

DB.closeStmt(pstmt);

DB.closeConn(conn);

}

return user;

}

/**

* 分页查询所有的用户数据

* @param pageNo 第几页

* @param pageSize 每页显示多少条

* @return pageModel对象

*/

public PageModel findAllUser(int pageNo, int pageSize) {

PageModel pageModel = null;

String sql ="select * from t_user where user_id 'root' order by user_id " +

"limit " + (pageNo - 1) * pageSize + ", " + pageSize;

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

conn = DB.getConn();

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

List userList = new ArrayList();

while (rs.next()) {

User user = new User();

user.setUserId(rs.getString("user_id"));

user.setUserName(rs.getString("user_name"));

user.setPassword(rs.getString("password"));

user.setContactTel(rs.getString("contact_tel"));

user.setEmail(rs.getString("email"));

user.setCreateDate(rs.getTimestamp("create_date"));

userList.add(user);

}

int totalRecords = getTotalRecords(conn);

pageModel = new PageModel();

pageModel.setPageNo(pageNo);

pageModel.setPageSize(pageSize);

pageModel.setTotalRecords(totalRecords);

pageModel.setList(userList);

}catch(SQLException e) {

e.printStackTrace();

}finally {

DB.closeRs(rs);

DB.closeStmt(stmt);

DB.closeConn(conn);

}

return pageModel;

}

/**

* 返回所有的用户数

* @param conn

* @return 记录数

*/

private int getTotalRecords(Connection conn) {

String sql = "select count(*) from t_user where user_id 'root'";

int totalRecords = 0;

Statement stmt = null;

ResultSet rs = null;

try {

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

if (rs.next()) {

totalRecords = rs.getInt(1);

}

}catch(SQLException e) {

e.printStackTrace();

}finally {

DB.closeRs(rs);

DB.closeStmt(stmt);

}

return totalRecords;

}

/**

* 根据用户ID删除用户

* @param userId userId

*/

public void deleteUserById(String userId) {

String sql = "delete from t_user where user_id=?";

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = DB.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, userId);

pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally {

DB.closeStmt(pstmt);

DB.closeConn(conn);

}

}

/**

* 根据UserId的集合进行删除

* @param userIds userId的集合

*

* 可以把jsp中的userId数组传过来,形成如下SQL:

* delete from t_user where user_id in ('a0001', 'a0002')

*/

public void deleteUserByIds(String[] userIds) {

}

}

如何用java编写用户信息管理模块

1. 用户信息管理一般包括:用户创建、用户修改、用户删除、用户查询四大块功能。

2. 定义基本实体类:User,包含name, address, idNum,tel,regTime四个属性和相应get/set方法。

3. 定义好后台数据库中的用户表user_T

4. 编写对用户进行增/删/改/查操作的数据库访问层代码

5. 编写对用户进行增/删/改/查操作前台页面,调用相应的后台方法即可。

具体代码?呵呵,有点太麻烦。先给点分再说吧。


分享标题:java用户管理类的代码,java用户管理类的代码有哪些
网页路径:http://cdiso.cn/article/phjhec.html

其他资讯