import?java.util.ArrayList;
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)潯陽免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
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("程序結(jié)束!");
System.exit(0);
break;
}
}
}
}
展開全部
類似于下面那樣連接數(shù)據(jù)庫:
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;
//聯(lián)系電話
private String contactTel;
private String email;
//創(chuàng)建日期
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);
}
}
/**
* 根據(jù)用戶代碼查詢
* @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;
}
/**
* 分頁查詢所有的用戶數(shù)據(jù)
* @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;
}
/**
* 返回所有的用戶數(shù)
* @param conn
* @return 記錄數(shù)
*/
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;
}
/**
* 根據(jù)用戶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);
}
}
/**
* 根據(jù)UserId的集合進行刪除
* @param userIds userId的集合
*
* 可以把jsp中的userId數(shù)組傳過來,形成如下SQL:
* delete from t_user where user_id in ('a0001', 'a0002')
*/
public void deleteUserByIds(String[] userIds) {
}
}
1. 用戶信息管理一般包括:用戶創(chuàng)建、用戶修改、用戶刪除、用戶查詢四大塊功能。
2. 定義基本實體類:User,包含name, address, idNum,tel,regTime四個屬性和相應(yīng)get/set方法。
3. 定義好后臺數(shù)據(jù)庫中的用戶表user_T
4. 編寫對用戶進行增/刪/改/查操作的數(shù)據(jù)庫訪問層代碼
5. 編寫對用戶進行增/刪/改/查操作前臺頁面,調(diào)用相應(yīng)的后臺方法即可。
具體代碼?呵呵,有點太麻煩。先給點分再說吧。