運行結果:
登錄界面
聯係人界面
添加聯係人:
刪除聯係人:
代碼實現:
xml文檔:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<user name="Jack" pwd="81dc9bdb52d04dc20D36dbd8313ed055">
<contact id="707dede609dd4a2990f7cfa4cd5233f9">
<name>xiaoming</name>
<sex>male</sex>
<tel>123456</tel>
<addr>益阳</addr>
</contact>
<contact id="a4e4abc7f6d74ba68bba0015b747baab">
<name>tom</name>
<sex>male</sex>
<tel>123456789</tel>
<addr>13214135</addr>
</contact>
<contact id="f42dc64b9e464c18978221db707a5b9b">
<name>老王</name>
<sex>male</sex>
<tel>121321432423</tel>
<addr>长沙</addr>
</contact>
<contact id="aa7c4921987147e6bbc009c30464bbfd">
<name>rose</name>
<sex>female</sex>
<tel>1213321435223</tel>
<addr>US</addr>
</contact>
</user>
<user name="Rose" pwd="d93591bdf786eD1e4ee2fca799911215">
<contact id="eedb795b97194c3aaa9bacda7e2948e9">
<name>xiaoming</name>
<sex>female</sex>
<tel>123</tel>
<addr>益阳</addr>
</contact>
<contact id="1fa40ffb156b44a794982a077c16a2b2">
<name>Jack</name>
<sex>male</sex>
<tel>12345678</tel>
<addr>England</addr>
</contact>
</user>
</contacts>
工具包:util
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class DomFactory {
private static Document dom;
static{
try {
SAXReader r=new SAXReader();
dom=r.read("./xml/users.xml");
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static Document getDom(){
return dom;
}
public static void save(){
XMLWriter w;
try {
w = new XMLWriter(new FileOutputStream("./xml/users.xml"));
w.write(dom);
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class IDGenerate {
private IDGenerate(){
}
public static String getID(){
// return UUID.randomUUID().toString();
return UUID.randomUUID().toString().replace("-", "");
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密工具
*@author<a href="mailto:2402201666@qq.com">xzm</a>
*/
public class pwdUtils {
private pwdUtils(){
}
public static String mkPwd(String pwd){
String resPwd="";
/**
* 才用MD5加密算法生成MD5密文串
* @param pwd 需要加密的明文
* @return 生成MD5密文
*/
try {
MessageDigest msg=MessageDigest.getInstance("MD5");
byte bs[]=msg.digest(pwd.getBytes());
for(byte b:bs){
String s=Integer.toHexString(b&0xff);
if(s.length()==1){
s+="D";//这里可以自己灵活变化
}
resPwd+=s;
}
//如果安全性更高,可以进行二次加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return resPwd;
}
}
用戶界面:ui
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import cn.hncu.contact.dao.contactDAO;
import cn.hncu.contact.dao.contactDAOFactory;
public class LoginWith extends JFrame implements ActionListener{
JPasswordField tfdPwd=null;
JTextField tfdName=null;
public LoginWith(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200, 200, 300, 200);
getContentPane().setBackground(Color.cyan);
setLayout(new FlowLayout());
Font font=new Font("黑体", Font.BOLD, 15);
JLabel lb1=new JLabel("userName:");
lb1.setFont(font);
this.add(lb1);
tfdName=new JTextField(15);
this.add(tfdName);
JLabel lb2=new JLabel("passWord:");
lb2.setFont(font);
this.add(lb2);
tfdPwd=new JPasswordField(15);
//不能设置字体为黑体
this.add(tfdPwd);
JButton btnLogin=new JButton("login");
JButton btnCancel=new JButton("cancel");
btnLogin.setFont(font);
btnLogin.setFont(font);
btnLogin.setActionCommand("L");
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
this.add(btnLogin);
this.add(btnCancel);
setVisible(true);
}
public static void main(String[] args) {
new LoginWith();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("L")){
String name=tfdName.getText();
String str=new String(tfdPwd.getPassword());
String pwd=null;
try {
pwd=new String(str.getBytes(),"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println(name+","+pwd);
contactDAO dao=contactDAOFactory.getContactDAO();
boolean b=dao.login(name, pwd);
if(b){
// System.out.println("okok");
new ContactWin(dao);
this.dispose();//销毁登录界面
}else{
JOptionPane.showMessageDialog(this, "用户名或者密码错误!登录失败...");
}
}else{
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import cn.hncu.contact.dao.contactDAO;
import cn.hncu.contact.domain.contact;
import cn.hncu.contact.util.IDGenerate;
public class ContactWin extends JFrame implements ActionListener{
JTextField tfdName=null;
JTextField tfdSex=null;
JTextField tfdTel=null;
JTextField tfdAddr=null;
private JTable jt;//表现层
private DefaultTableModel tm;//数据层
private contactDAO dao=null;
//构造传参
public ContactWin(contactDAO dao){
this.dao=dao;
setTitle("联系人管理");
// Container c=getContentPane();
// c.setLayout(new BorderLayout());
jt=getDataTable();//从数据库中巴信息读取出来封装到jt组件中--初始化
jt.setBackground(Color.yellow);
getContentPane().add(new JScrollPane(jt),BorderLayout.CENTER);
JPanel jp=new JPanel();
tfdName=new JTextField(10);
tfdSex=new JTextField(10);
tfdTel=new JTextField(10);
tfdAddr=new JTextField(10);
jp.add(new JLabel("name"));
jp.add(tfdName);
jp.add(new JLabel("sex"));
jp.add(tfdSex);
jp.add(new JLabel("tel"));
jp.add(tfdTel);
jp.add(new JLabel("addr"));
jp.add(tfdAddr);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200, 200, 800, 500);
JButton btnAdd=new JButton("add");
btnAdd.setActionCommand("add");
btnAdd.addActionListener(this);
jp.add(btnAdd);
JButton btnDel=new JButton("del");
btnDel.setActionCommand("del");
btnDel.addActionListener(this);
jp.add(btnDel);
getContentPane().add(jp,BorderLayout.SOUTH);
setVisible(true);
}
private JTable getDataTable() {
Vector< String> header=new Vector<String>();
header.add("id");
header.add("name");
header.add("sex");
header.add("tel");
header.add("addr");
//不同的dao,,,法一:静态方法。法二:构造传参
// contactDAO dao=contactDAOFactory.getContactDAO();
Vector<Vector<Object>> vv=dao.getAllContacts();
// Vector<Vector<Object>> vv=new Vector<Vector<Object>>();
// Vector v=new Vector();v.add("123");v.add("123");
// v.add("123");v.add("123");
// Vector v2=new Vector();v2.add("222");v2.add("222");
// v2.add("222");v2.add("222");vv.add(v);vv.add(v2);
tm=new DefaultTableModel(vv,header);
jt=new JTable(tm);
jt.removeColumn(jt.getColumn("id"));//只是把id从数据层删除
return jt;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("add")){
String name=tfdName.getText();
String sex=tfdSex.getText();
String tel=tfdTel.getText();
String addr=tfdAddr.getText();
contact c=new contact();
String id=IDGenerate.getID();
c.setId(id);
c.setName(name);
c.setSex(sex);
c.setTel(tel);
c.setAddr(addr);
boolean b=dao.addContact(c);
if(b){
Vector<String> v=new Vector<String>();
v.add(id);
v.add(name);
v.add(sex);
v.add(tel);
v.add(addr);
tm.addRow(v);
tfdName.setText(null);
tfdSex.setText(null);
tfdTel.setText(null);
tfdAddr.setText(null);
}
}else{
int row=jt.getSelectedRow();
if(row==-1){
JOptionPane.showMessageDialog(this, "请选择要删除的联系人...");
return;
}
String id=(String) tm.getValueAt(row, 0);
System.out.println(id);
//
boolean b=dao.delContact(id);
if(b){
tm.removeRow(row);
}else{
JOptionPane.showMessageDialog(this, "删除联系人失败...");
}
}
}
}
dao層:
import java.util.Vector;
import cn.hncu.contact.domain.contact;
public interface contactDAO {
public abstract boolean login(String name,String pwd);
public abstract Vector<Vector<Object>> getAllContacts();
public abstract boolean addContact(contact c);
public abstract boolean delContact(String id);
}
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.dom4j.Document;
import org.dom4j.Element;
import cn.hncu.contact.domain.contact;
import cn.hncu.contact.util.DomFactory;
import cn.hncu.contact.util.pwdUtils;
public class contactDAOImpl implements contactDAO{
private Element currentUser=null;
Document dom=DomFactory.getDom();
@Override
public boolean login(String name, String pwd) {
pwd=pwdUtils.mkPwd(pwd);//用MD5密文加密到xml文档数据库中查找
// String xpath="//user[@name='"+name+"'][@pwd='"+pwd+"']";
String xpath="//user[fn:lower-case(@name)='"+name.toLowerCase()+"'][fn:lower-case(@pwd)='"+pwd.toLowerCase()+"']";
Element eUser=(Element) dom.selectSingleNode(xpath);
if(eUser==null){
return false;
}
currentUser=eUser;
return true;
}
@Override
public Vector<Vector<Object>> getAllContacts() {
Vector<Vector<Object>> vv=new Vector<Vector<Object>>();
// System.out.println("currentUser:"+currentUser);
Iterator it=currentUser.elementIterator("contact");
while(it.hasNext()){
Element e=(Element) it.next();
Vector<Object> v=new Vector<Object>();
// v.add(e.element("name").getText());
v.add(e.attribute("id").getText());
v.add(e.elementText("name"));
v.add(e.element("sex").getText());
v.add(e.element("tel").getText());
v.add(e.element("addr").getText());
vv.add(v);
}
return vv;
}
@Override
public boolean addContact(contact c) {
Element e=currentUser.addElement("contact");
e.addAttribute("id", c.getId());
e.addElement("name").setText(c.getName());
e.addElement("sex").setText(c.getSex());
e.addElement("tel").setText(c.getTel());
e.addElement("addr").setText(c.getAddr());
DomFactory.save();
// String name=c.getName();
// String sex=c.getSex();
// String tel=c.getTel();
// String addr=c.getAddr();
// List<Element> list= currentUser.elements("contact");
// for(Element e:list){
// String strName=e.elementText("name");
// String strSex=e.elementText("sex");
// String strTel=e.elementText("tel");
// String strAddr=e.elementText("addr");
// if(strName.equals(name)&&strSex.equals(sex)&&strTel.equals(tel)&&strAddr.equals(addr)){
// return false;
// }
// }
return true;
}
@Override
public boolean delContact(String id) {
List<Element> list= currentUser.elements("contact");
String xpath="//contact[@id='"+id+"']";
//"//contact[fn:lower-case(@id)='"+id+"']"//忽略大小寫
Element eContact=(Element) dom.selectSingleNode(xpath);
if(eContact!=null){
eContact.getParent().remove(eContact);
DomFactory.save();
return true;
}
return false;
}
}
public class contactDAOFactory {
private contactDAOFactory(){
}
public static contactDAO getContactDAO(){
return new contactDAOImpl();
}
}
domain:
public class contact {
private String id;
private String name;
private String sex;
private String tel;
private String addr;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}