这个通讯录系统是有我和另一位同学共同实现,由于我源码丢失,因此转载该同学的文章。
2015-2016学年第1学期
《Java言程序设计(英)》课程大作业报告
课题名称: 《个人通讯录》
小组成员人数(单选)□1人 ■2人 □3人
1. 软件系统分析
本软件是一个简化的联系人信息管理软件,通过注册本软件使用存储联系人信息的功能。
(1)通讯录功能模块
要实现的功能分别为用户管理、用户登录、添加联系人、修改联系人、删除联系人、查找联系人。如图:
(2)数据库内容
建立两个数据表分别为,用户表和联系人表
1、用户表中包含用户的ID、姓名、密码三变量,用于储存用户登录和注册信息。
2、联系人表包含编号、姓名、性别、年龄、电话、Email、邮编、传真、地址、分组、照片和备注等信息。
用户表和联系人表的关系如图:
图1.1 功能创新性成像
关系如图1.2:
图1.2数据库关系图
(3)软件实现相关功能的流程图
①登录窗体的实现如图1.3
图1.3登陆窗口实现
登录窗体中可以实现用户的登录和注册功能,当用户输入用户名和密码时点击“登录”按钮,然后调用数据库,若数据库中已有该用户且密码正确则登陆成功并进入主界面,否则重新提示“用户名错误或密码错误”。当点击“注册”按钮时,调用数据库,若已有相同用户则提示“”已注册,清空文本框;若数据库中没有该用户则存入该用户信息到数据库中。
②主窗体实现如图1.4
在主窗体中可以实现以下功能,用户联系人的新增、删除、修改和搜索联系人信息
图1.4主窗体实现
算法分析
Login类
这个类主要实现的是登入窗口的方法,在主窗体里设置了两个文本框用于获得用户名和密码。然后设置两个按钮分别是登陆和注册。关于登陆按钮会先检查二个文本框是否为空,如果是就报错否则调用access数据库查询是否信息正确。关于注册按钮,当用户名密码不为空的时候,检测数据库里没有同名联系人的情况下将新建一个账号。
MainFram类
这个类是通讯录的主窗口,这个类定义了许多文本框和按钮分别储存显示用户当前联系人的编号、姓名、性别、年龄、电话、Email、邮编、传真、地址、所属分组、照片和备注。还有许多按钮我们为其增加了相应的监听来完成不同的需求。
新增按钮:点击将会把模式切换到新增,在点击保存的时候会有所改变。
搜索按钮:当前支持两种模式的查询一个是编号,一个是姓名。点击后调用sql语句对数据库进行访问,如果存在该联系人通过setInfo(DBUtil.getPerInfo());的方法从数据库中获得消息然后再把消息显示在信息面板中否则提示没有该联系人。
添加分组按钮:点击后会弹出对话框,输入想要添加的分组,如果有就提示错误否则进一步确认然后添加新的分组。
删除分组按钮:点击后会提醒是否删除,确认后会调用sql语言访问数据库删除lxy表中所有含当前分组的联系人。
浏览按钮:点击浏览按钮会弹出一个新窗口可以访问本机文件,具体情况在Browse_pictures类里在做解释。
保存按钮:点保存按钮会先判断当前模式,如果是新增就把从信息面板中的获得的信息在数据库中新建记录,如果是修改的话就会先找到联系人然后再数据库里更新其信息。
删除按钮:点击会先弹出窗口寻求确认,确认后会调用sql语句把当前面板的联系人删除。
其他重要的变量和方法:
public static String picPath :表示当前联系人的照片的绝对路径。
public static String nowID :表示当前的联系人id用于类外调用。
public void iniTopPanel(); 上面板的初始化
public void iniBelowPanel(); 下面板的初始化
public void iniPhoPanel(); 照片面板的设置
public void actionPerformed(); 为每个按钮注册监听器
public void clearInfo(); 清空信息面板
public voidsetInfo(Vector<String>pInfo); 将信息向量设置到信息面板中
public voidgetInfo(Vector<String>pInfo); 从信息面板中获得信息向量
public setPic(String pid);设置个人图像从数据库中获得当前联系人照片的绝对路径然后显示出来
public void getnowID(); 获得当前用户编号
3.DBUtil类
这个类是连接数据库和调用数据库,我们采用Access数据库。
首先是连接到数据库的方式有两种,一种是搭建ODBC数据源然后连接,另外一是通过直接路径直接连接。
调用Class.forName(driver);driver=sun.jdbc.odbc.JdbcOdbcDriver;
然后是声明数据库的引用DriverManager.getConnection(url);url=jdbc:odbc:driver={MicrosoftAccess Driver (*.mdb)};DBQ=path;其中path为数据库存放的绝对路径。
然后是一些主要的方法:
publicstatic Connection getConnection();连接数据库的方法就是上面描述一样
publicstatic void closeCon(); 关闭数据库的连接
publicstatic boolen check(String user,String pwd); 登录的验证判断用户名密码是否正确
publicstatic int update(String sql); 根据传来的sql语句更新数据库
publicstatic boolen isExist(String sql); 判断该记录是否存在
publicstatic int delUser(String uid); 删除该用户
publicstatic void delGroup(String user,String group); 删除分组返回删除的联系人个数
publicstatic String insertPerson(String uid,Vector<String> pInfo);根据信息向量添加联系人
publicstatic String updatePerson(String uid,Vector<String>pInfo);根据信息向量更新联系人
publicstatic void updatePic(String sql,String photo); 更新照片的地址
publicstatic Vector<String> getPerInfo(String sql); 获得联系人信息
publicstatic String getPic(String sql); 得到照片
4.Browse_pictures类
该类是用于浏览电脑本地文件夹然后选择照片,当选择好照片后有两个按钮供选择。
确定按钮:获得当前联系人id然后获得当前照片路径更新到数据库。
取消按钮:取消当前的选择,返回通讯录界面。
3.软件和代码的实现
(1)登陆窗口关键技术的实现 Login.java
-
(
1)登陆窗口关键技术的实现 Login.java
-
/******* 确定按钮设置和监听事件 *********/
-
btnInt.addActionListener(
new ActionListener() {
-
public void actionPerformed(ActionEvent e) {
-
// 登录按钮响应事件
-
String user = jbf.getText().trim();
// 获得用户名
-
String pwd = String.valueOf(jpf.getPassword());
// 获得密码
-
if (DBUtil.check(user, pwd)) {
-
JOptionPane.showMessageDialog(
null,
"登入成功");
-
MainFram mf =
new MainFram(user);
-
setVisible(
false);
-
}
else {
-
JOptionPane.showMessageDialog(
null,
"对不起用户名或密码错误");
-
clear();
-
}
-
}
-
-
});
-
/************* 注册按钮设置和响应事件 ************/
-
btnSin.addActionListener(
new ActionListener() {
-
public void actionPerformed(ActionEvent e) {
-
// 注册按钮响应事件
-
String user = jbf.getText().trim();
// 获得用户名
-
String pwd = String.valueOf(jpf.getPassword());
// 获得密码
-
String sql =
"";
// 声明sql语句
-
if (user.equals(
"")) {
-
JOptionPane.showMessageDialog(
null,
"用户名不能为空");
-
}
else
if (pwd.equals(
"")) {
-
JOptionPane.showMessageDialog(
null,
"密码不能为空");
-
}
else {
-
sql =
"select uid from user where uid='" + user +
"'";
-
if (DBUtil.isExist(sql)) {
// 用户名已经存在
-
JOptionPane.showMessageDialog(
null,
"对不起,用户名已存在!!!");
-
clear();
// 清空输入文本框
-
}
else {
-
sql =
"insert into user values('" + user +
"','" + pwd
-
+
"')";
-
if (DBUtil.update(sql) >
0) {
// 注册成功
-
JOptionPane.showMessageDialog(
null,
-
"恭喜您!!!注册成功,请登陆");
-
clear();
-
}
-
}
-
}
-
}
-
});
-
-
}
-
(
2)主窗口关键技术的实现MainFram.java
-
//窗体的初始化
-
public void actionPerformed(ActionEvent e) {
// 按钮注册事件的实现
-
if (e.getSource() == jbtAdd) {
-
monitorAddButton();
-
}
else
if (e.getSource() == jbtSearch) {
-
monitorSearchButton();
-
}
else
if (e.getSource() == jbtAddGroup) {
-
monitorAddGroupButton();
-
}
else
if (e.getSource() == jbtDeleteGroup) {
-
monitorDeleteGroupButton();
-
}
else
if (e.getSource() == jbtBrowse) {
-
monitorBrowseButton();
-
}
else
if (e.getSource() == jbtSave) {
-
monitorSaveButton();
-
}
else
if (e.getSource() == jbtdelete) {
-
monitorDelButton();
-
}
-
}
-
public void clearInfo()// 清空信息面板
-
{
-
TFName.setText(
"");
// 清空文本框
-
TFAge.setText(
"");
-
TFidentifier.setText(
"");
-
TFTel.setText(
"");
-
TFEmail.setText(
"");
-
TFPostcode.setText(
"");
-
TFAdress.setText(
"");
-
TFFax.setText(
"");
-
TFChImg.setText(
"");
-
beizhuTA.setText(
"");
-
JPimg.setIcon(
null);
// 清空图像
-
jbtComboBox.setSelectedItem(
"其它");
// 设置分组选择“其它分组”
-
picPath =
"";
// 图片路径设置为空
-
}
-
public Vector<String> getInfo()// 从信息面板得到用户输入的信息
-
-
{
-
Vector<String> pInfo =
new Vector<String>();
-
pInfo.add(TFidentifier.getText().trim());
// 编号
-
pInfo.add(TFName.getText().trim());
// 添加name
-
String gender = RB_man.isSelected() ?
"男" :
"女";
-
pInfo.add(gender);
// 添加性别
-
pInfo.add(TFAge.getText().trim());
// 年龄
-
pInfo.add(TFTel.getText().trim());
// 电话号码
-
pInfo.add(TFEmail.getText().trim());
// Email
-
pInfo.add((String) jbtComboBox.getSelectedItem());
// 分组
-
pInfo.add(TFPostcode.getText().trim());
// 邮编
-
pInfo.add(TFFax.getText());
// 传真
-
pInfo.add(beizhuTA.getText());
// 备注
-
pInfo.add(TFAdress.getText().trim());
// 地址
-
pInfo.add(TFName.getText().trim());
// 名字
-
String photoPath = picPath;
// 得到照片路径
-
pInfo.add(photoPath);
// 照片路径
-
return pInfo;
-
}
-
public void setInfo(Vector<String> pInfo) {
-
// 将信息向量按规则填到信息面板里
-
this.clearInfo();
-
if (pInfo.size() ==
0) {
-
JOptionPane.showMessageDialog(
this,
"所查用户不存在!!!",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
else {
// 显示联系姓名
-
String out;
-
out = pInfo.get(
1);
-
JOptionPane.showMessageDialog(
null, out);
-
TFidentifier.setText(pInfo.get(
0));
// 编号
-
TFName.setText(pInfo.get(
1));
// 姓名
-
if (pInfo.get(
2).equals(
"男")) {
// 显示性别
-
RB_man.setSelected(
true);
-
}
else {
// 显示性别
-
RB_femal.setSelected(
true);
-
}
-
// 显示年龄、电话号码和电子邮件
-
TFAge.setText(pInfo.get(
3));
-
TFTel.setText(pInfo.get(
4));
-
TFEmail.setText(pInfo.get(
5));
-
-
String group = pInfo.get(
6);
// 分组
-
int group_number = jbtComboBox.getItemCount();
// 分组个数
-
boolean isExist =
false;
-
for (
int i =
0; i < group_number && isExist; ++i) {
-
if (group.equals((String) jbtComboBox.getItemAt(i))) {
-
jbtComboBox.setSelectedItem(pInfo.get(
6));
// 设置分组信息
-
isExist =
true;
-
}
-
}
-
if (!isExist) {
// 初始化分组中不存在该分组
-
jbtComboBox.addItem(group);
// 添加分组
-
jbtComboBox.setSelectedItem(pInfo.get(
6));
// 设置分组信息
-
}
-
// 显示邮编和地址
-
TFPostcode.setText(pInfo.get(
7));
-
TFFax.setText(pInfo.get(
8));
// 传真
-
beizhuTA.setText(pInfo.get(
9));
// 备注
-
TFAdress.setText(pInfo.get(
10));
// 地址
-
setPic(pInfo.get(
0));
// 设置图像为从数据库得到的图像
-
}
-
}
-
public void setPic(String pid) {
// 设置个人图像显示
-
String sql =
"select pphoto from lxy where pid='" + pid +
"'";
-
String Pic = DBUtil.getPic(sql);
// 从数据库得到此人的图像
-
if (Pic !=
null) {
// 如果此联系人上传了图像
-
picPath = Pic;
-
JPimg.setIcon(
new ImageIcon(Pic));
// 将图像显示到JLabel
-
JPimg.setHorizontalAlignment(JLabel.CENTER);
// 设置图片水平居中显示
-
JPimg.setVerticalAlignment(JLabel.CENTER);
// 设置图片垂直方向居中显示
-
}
else {
// 如果图像为空,则不显示
-
JPimg.setIcon(
null);
-
}
-
}
-
public void monitorAddButton()// 监听新增按钮的方法
-
{
-
JOptionPane.showMessageDialog(
null,
"请输入信息后保存!");
-
isInsert =
true;
-
clearInfo();
-
};
-
-
public void monitorSearchButton() {
// 监听搜索按钮的方法
-
String search = TFSearch.getText().trim();
// 获得搜索框内容
-
String mode = IDsearche.isSelected() ?
"编号" :
"姓名";
-
String sql =
"";
// 声明查找字符串
-
if (search.equals(
"")) {
-
JOptionPane.showMessageDialog(
this,
"查找条件不能为空!!!",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
else {
-
if (mode.equals(
"姓名")) {
-
sql =
"select pid,pname,pgender,page,pnumber,pemail,pgroup,ppostalcode,pfax,premarks,padress from lxy where uid='"
-
+ nowname +
"'and pname='" + search +
"'";
-
}
else {
-
sql =
"select pid,pname,pgender,page,pnumber,pemail,pgroup,ppostalcode,pfax,premarks,padress from lxy where uid='"
-
+ nowname +
"'and pid='" + search +
"'";
-
}
-
setInfo(DBUtil.getPerInfo(sql));
// 设置信息面板为该联系人的信息
-
}
-
};
-
public void monitorAddGroupButton() {
// 监听添加分组按钮的方法
-
String group = JOptionPane.showInputDialog(
this,
"请输入分组:",
"添加分组", JOptionPane.PLAIN_MESSAGE);
-
if (group !=
null) {
// 当分组不为空的时候
-
if (group.equals(
"")) {
-
JOptionPane.showMessageDialog(
this,
"分组名字不能为空",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
else {
-
int group_number = jbtComboBox.getItemCount();
// 分组个数
-
// JOptionPane.showMessageDialog(this,"分组有"+group_number+"个","错误",JOptionPane.WARNING_MESSAGE);
-
for (
int i =
0; i < group_number; ++i) {
-
if (group.equals((String) jbtComboBox.getItemAt(i))) {
// 分组已存在,直接返回
-
JOptionPane.showMessageDialog(
this,
"分组已存在",
"错误", JOptionPane.WARNING_MESSAGE);
-
return;
-
}
-
}
-
jbtComboBox.addItem(group);
// 添加分组
-
JOptionPane.showMessageDialog(
this,
"分组添加成功",
"提示", JOptionPane.WARNING_MESSAGE);
// 添加分组成功
-
jbtComboBox.setSelectedItem(group);
// 选中当前分组
-
}
-
}
-
};
-
-
public void monitorDeleteGroupButton() {
// 监听删除分组按钮的方法
-
int select = JOptionPane.showConfirmDialog(
this,
"删除分组将会删除" +
"分组里的所有联系人!!!是否删除?",
"确认",
-
JOptionPane.YES_NO_OPTION);
-
if (select == JOptionPane.YES_OPTION) {
// 确定删除联系人
-
String group = (String) jbtComboBox.getSelectedItem();
-
DBUtil.delGroup(nowname, group);
// 从数据库中删除
-
jbtComboBox.removeItem((String) jbtComboBox.getSelectedItem());
// 从下拉列表中删除
-
JOptionPane.showMessageDialog(
this,
"删除分组成功",
"消息", JOptionPane.INFORMATION_MESSAGE);
-
}
-
clearInfo();
-
};
-
-
public void monitorBrowseButton() {
// 监听浏览按钮的方法
-
getnowID();
-
EventQueue.invokeLater(
new Runnable() {
-
@Override
-
public void run() {
-
// TODO Auto-generated method stub
-
JFrame frame =
new Browse_pictures();
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.setVisible(
true);
-
}
-
});
-
};
-
-
public void monitorSaveButton() {
// 监听保存按钮的方法
-
String mode;
-
if (isInsert)
-
mode =
"添加";
-
else
-
mode =
"修改";
-
JOptionPane.showMessageDialog(
this, mode, mode, JOptionPane.WARNING_MESSAGE);
-
String pid = TFidentifier.getText().trim();
// 得到联系人的编号
-
String pname = TFName.getText().trim();
// 得到联系人的姓名
-
String sqla =
"select * from lxy where pid='" + pid +
"'";
// 判断此编号是否存在的SQL
-
String sqlb =
"select * from lxy where pname='" + pname +
"'";
// 判断此姓名是否存在的SQL
-
boolean isIdExist = DBUtil.isExist(sqla);
// 得到编号是否存在
-
boolean isNameExist = DBUtil.isExist(sqlb);
// 得到姓名是否存在
-
String perNameAfter = TFName.getText().trim();
// 得到修改后联系人的名字
-
if (!(pid.equals(
"") || pname.equals(
""))) {
-
if (isInsert) {
// 是添加的话
-
JOptionPane.showMessageDialog(
null,
"现在开始添加");
-
if (!(pid.equals(
"") || pname.equals(
""))) {
// 得到插入的图片路径是否合法的判断字符串
-
String s = DBUtil.insertPerson(nowname, getInfo());
-
if (s.equals(
"isNull")) {
-
JOptionPane.showMessageDialog(
this,
"图像路径不合法" +
"添加联系人失败",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
-
}
else {
// 用户名或者ID已经存在
-
JOptionPane.showMessageDialog(
this,
"联系人ID或者姓名已经存在",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
-
}
else {
// 编辑联系人资料
-
JOptionPane.showMessageDialog(
null,
"现在开始修改");
-
if (isIdExist) {
// ID没变进行更新
-
String s = DBUtil.updatePerson(nowname, getInfo());
-
if (s.equals(
"isNull")) {
-
JOptionPane.showMessageDialog(
this,
"图像路径不合法" +
"添加联系人失败",
"错误", JOptionPane.WARNING_MESSAGE);
-
}
-
}
-
if (!isIdExist) {
// ID变了
-
if (!isNameExist) {
// 名字也变了就执行插入动作
-
String s = DBUtil.insertPerson(nowname,
this.getInfo());
-
if (!s.equals(
"isNull")) {
// 在树上添加节点 此为合法的文件路径
-
// this.addPerNode(perNameAfter,perGroupAfter);
-
}
else {
-
JOptionPane.showMessageDialog(
this,
"图像路径不合法" +
"添加联系人失败",
"错误",
-
JOptionPane.WARNING_MESSAGE);
-
}
-
}
else {
-
JOptionPane.showMessageDialog(
null,
"重新插入此人信息");
-
DBUtil.update(
"delete from lxy where pname='" + perNameAfter +
"'");
-
DBUtil.insertPerson(nowname,
this.getInfo());
// 重新插入此人记录
-
}
-
}
-
}
-
}
else {
// 为空
-
JOptionPane.showMessageDialog(
this,
"联系人ID或者姓名不能为空",
"错误", JOptionPane.WARNING_MESSAGE);
-
return;
-
}
-
isInsert =
false;
-
}
-
-
public void monitorDelButton()// 监听删除按钮的方法
-
{
-
String pid = TFidentifier.getText();
-
// 弹出删除对话框
-
int index = JOptionPane.showConfirmDialog(
this,
"是否删除?y/n",
"确认对话框", JOptionPane.YES_NO_OPTION,
-
JOptionPane.QUESTION_MESSAGE);
-
if (index ==
0) {
// 确认删除
-
String sqlb =
"delete from lxy where pid='" + pid +
"'";
-
int i = DBUtil.update(sqlb);
// 删除联系人信息
-
if (i != -
1) {
// 删除成功对话框
-
JOptionPane.showMessageDialog(
this,
"删除成功",
"删除", JOptionPane.INFORMATION_MESSAGE);
-
clearInfo();
-
}
-
}
-
-
}
-
public void getnowID() {
// 获得当前用户编号
-
nowID = TFidentifier.getText().trim();
-
}
-
}
-
(
3)浏览按钮功能的实现(修改头像方法)
-
class Browse_pictures extends JFrame {
-
JPanel buttonPanel =
new JPanel();
// 按钮面板
-
JButton jbmakesure =
new JButton(
"确定");
-
JButton jbcanel =
new JButton(
"取消");
-
-
public Browse_pictures() {
-
setTitle(
"ImageViewer");
-
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
-
setLocationRelativeTo(
null);
-
label =
new JLabel();
-
add(label);
-
chooser =
new JFileChooser();
-
chooser.setCurrentDirectory(
new File(
"."));
-
JMenuBar menubar =
new JMenuBar();
-
setJMenuBar(menubar);
-
JMenu menu =
new JMenu(
"File");
-
menubar.add(menu);
-
JMenuItem openItem =
new JMenuItem(
"Open");
-
menu.add(openItem);
-
JMenuItem exitItem =
new JMenuItem(
"Close");
-
menu.add(exitItem);
-
buttonPanel.add(jbmakesure);
-
buttonPanel.add(jbcanel);
-
jbmakesure.addActionListener(
new ActionListener() {
// 确定按钮监听
-
public void actionPerformed(ActionEvent arg0) {
-
int index = JOptionPane.showConfirmDialog(
null,
"是否修改图片?y/n",
"确认对话框", JOptionPane.YES_NO_OPTION,
-
JOptionPane.QUESTION_MESSAGE);
-
if (index ==
0) {
// 确认修改
-
String id = MainFram.nowID;
-
String sql =
"update lxy set pphoto=? where pid='" + id +
"'";
-
String photo = chooser.getSelectedFile().getPath();
-
DBUtil.updatePic(sql, photo);
-
MainFram.picPath = photo;
-
MainFram.JPimg.setIcon(
new ImageIcon(photo));
// 将图像显示到JLabel
-
MainFram.JPimg.setHorizontalAlignment(JLabel.CENTER);
// 设置图片水平居中显示
-
MainFram.JPimg.setVerticalAlignment(JLabel.CENTER);
// 设置图片垂直方向居中显示
-
}
-
}
-
});
-
jbcanel.addActionListener(
new ActionListener() {
// 取消事件监听
-
-
@Override
-
public void actionPerformed(ActionEvent arg0) {
-
dispose();
-
}
-
});
-
openItem.addActionListener(
new ActionListener() {
-
-
@Override
-
public void actionPerformed(ActionEvent arg0) {
-
// TODO Auto-generated method stub
-
int result = chooser.showOpenDialog(
null);
-
if (result == JFileChooser.APPROVE_OPTION) {
-
String name = chooser.getSelectedFile().getPath();
-
label.setIcon(
new ImageIcon(name));
-
// 显示按钮面板
-
add(buttonPanel, BorderLayout.SOUTH);
-
-
}
-
}
-
});
-
exitItem.addActionListener(
new ActionListener() {
-
-
@Override
-
public void actionPerformed(ActionEvent arg0) {
-
// TODO Auto-generated method stub
-
setVisible(
false);
-
}
-
});
-
}
-
private JLabel label;
-
private JFileChooser chooser;
-
private
static
final
int DEFAULT_WIDTH =
300;
-
private
static
final
int DEFAULT_HEIGHT =
400;
-
}
-
(
4)数据库的使用的实现
-
public
class DBUtil {
-
private
static String driver =
"sun.jdbc.odbc.JdbcOdbcDriver";
// 声明驱动类字符串
-
private
static String url =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db\\linkman.mdb";
// 声明数据库连接字符串
-
private
static Connection con =
null;
// 数据库连接引用
-
private
static Statement stat =
null;
// 语句引用
-
private
static PreparedStatement psInsert =
null;
// 预编译语句引用
-
private
static ResultSet rs =
null;
// 结果及对象的引用
-
-
public static Connection getConnection() {
// 连接数据库的方法
-
try {
-
Class.forName(driver);
-
con = DriverManager.getConnection(url);
-
}
catch (Exception e) {
-
System.out.println(
"连接失败!");
-
e.printStackTrace();
-
}
-
return con;
// 返回连接
-
}
-
-
public static void closeCon() {
// 关闭数据库连接的方法
-
try {
-
if (rs !=
null) {
-
rs.close();
-
rs =
null;
-
}
-
if (stat !=
null) {
-
stat.close();
-
stat =
null;
-
}
-
if (con !=
null) {
-
con.close();
-
con =
null;
-
}
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
public static boolean check(String user, String pwd) {
// 登陆验证
-
boolean flag =
false;
-
try {
-
con = DBUtil.getConnection();
// 得到数据库连接
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(
"select pwd from user where uid='" + user +
"'");
-
rs.next();
-
String mywd = rs.getString(
1);
// 得到密码
-
if (mywd.equals(pwd)) {
-
flag =
true;
// 密码正确
-
}
-
}
catch (Exception e) {
-
flag =
false;
// 有异常登录失败
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return flag;
-
}
-
public static int update(String sql) {
// 更新数据库
-
int res =
0;
// 返回值
-
try {
-
con = DBUtil.getConnection();
// 连接到数据库
-
stat = con.createStatement();
// 创建语句对象
-
res = stat.executeUpdate(sql);
// 更新
-
}
catch (Exception e) {
-
e.printStackTrace();
// 异常输出
-
res = -
1;
// 返回值为负一
-
}
-
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return res;
-
}
-
-
public static boolean isExist(String sql) {
// 判断是否存在该记录
-
boolean flag =
false;
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(sql);
// 查询
-
if (rs.next()) {
-
flag =
true;
// 存在
-
}
-
}
catch (Exception e) {
-
e.printStackTrace();
-
flag =
false;
-
}
finally {
-
DBUtil.closeCon();
-
}
-
return flag;
-
}
-
-
public static int delUser(String uid) {
// 删除用户
-
int res =
0;
-
Vector<String> mypid =
new Vector<String>();
// 用于存放用户联系人的向量集合
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(
"select pid from lxy where uid='" + uid +
"'");
// 得到联系人id
-
while (rs.next()) {
-
String myid = rs.getString(
1);
-
mypid.add(myid);
-
}
-
stat = con.createStatement();
// 创建语句对象
-
res = stat.executeUpdate(
"delete from lxy where uid='" + uid +
"'");
// 从lxy表中删除每个联系人
-
stat.executeUpdate(
"delete from user where uid='" + uid +
"'");
// 从use表中删除用户
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return res;
// 返回删除多少联系人
-
}
-
-
public static void delGroup(String user, String group) {
// 删除分组,返回联系人的个数
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(
"select pid from lxy where pgroup='" + group +
"'" +
"and uid='" + user +
"'");
// 从数据库中搜索group的pid
-
stat = con.createStatement();
// 创建语句对象
-
stat.executeUpdate(
"delete from lxy where pgroup='" + group +
"'");
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印异常信息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库连接
-
}
-
}
-
-
public static String insertPerson(String uid, Vector<String> pInfo) {
// 添加联系人
-
String isPathNull =
"isNotNull";
// 图像是否合法
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
if (pInfo.get(
11).equals(
"") || pInfo.get(
11) ==
null) {
// 路径为空
-
psInsert = con.prepareStatement(
-
"insert into lxy(pid,pname,pgender,page,pnumber,pemail,pgroup,ppostalcode,pfax,premarks,padress,uid)values(?,?,?,?,?,?,?,?,?,?,?,?)");
-
}
else {
-
psInsert = con.prepareStatement(
-
"insert into lxy(pid,pname,pgender,page,pnumber,pemail,pgroup,ppostalcode,pfax,premarks,padress,uid,pphoto)values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
-
psInsert.setString(
13, pInfo.get(
12));
// 设置pphoto参数的数据
-
}
-
for (
int i =
0; i <
11; i++) {
// 设置公共信息
-
psInsert.setString(i +
1, pInfo.get(i));
-
}
-
psInsert.setString(
12, uid);
// 用户
-
psInsert.execute();
// 执行更新
-
psInsert.close();
// 关闭语句
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印错误信息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return isPathNull;
-
}
-
-
public static String updatePerson(String uid, Vector<String> pInfo) {
// 更新联系人
-
String isPathNull =
"isNotNull";
// 图像是否合法
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
if (pInfo.get(
11).equals(
"") || pInfo.get(
11) ==
null) {
// 路径为空
-
System.out.println(
"rujinweikon");
-
psInsert = con.prepareStatement(
-
"update lxy set pname=?,pgender=?,page=?,pnumber=?,pemail=?,pgroup=?,ppostalcode=?,pfax=?,premarks=?,padress=?,uid=? where pid='"
-
+ pInfo.get(
0).trim() +
"'");
-
}
else {
-
psInsert = con.prepareStatement(
-
"update lxy set pname=?,pgender=?,page=?,pnumber=?,pemail=?,pgroup=?,ppostalcode=?,pfax=?,premarks=?,padress=?,uid=?,pphoto=? where pid='"
-
+ pInfo.get(
0).trim() +
"'");
-
psInsert.setString(
12, pInfo.get(
11));
// 设置pphoto参数的数据
-
}
-
for (
int i =
0; i <
10; i++) {
// 设置公共信息
-
psInsert.setString(i +
1, pInfo.get(i +
1));
-
}
-
System.out.println(uid);
-
psInsert.setString(
11, uid);
// 用户
-
psInsert.execute();
// 执行更新
-
psInsert.close();
// 关闭语句
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印错误信息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return isPathNull;
-
}
-
-
public static void updatePic(String sql, String photo) {
-
try {
-
con = DBUtil.getConnection();
// 连接数据库
-
stat = con.createStatement();
// 创建语句对象
-
psInsert = con.prepareStatement(sql);
-
psInsert.setString(
1, photo);
-
psInsert.execute();
// 执行更新
-
psInsert.close();
// 关闭语句
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印错误信息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
}
-
-
public static Vector<String> getPerInfo(String sql) {
// 得到联系人信息
-
Vector<String> pInfo =
new Vector<String>();
-
try {
-
con = DBUtil.getConnection();
// 连接到数据库
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(sql);
// 查询
-
while (rs.next()) {
-
for (
int i =
1; i <
12; ++i) {
-
pInfo.add(rs.getString(i));
// 添加到返回集合
-
}
-
}
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印异常消息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return pInfo;
-
}
-
-
public static String getPic(String sql) {
// 得到照片
-
String resPic =
"";
-
try {
-
con = DBUtil.getConnection();
// 连接到数据库
-
stat = con.createStatement();
// 创建语句对象
-
rs = stat.executeQuery(sql);
// 执行sql语句
-
if (rs.next()) {
-
resPic = rs.getString(
1);
-
}
-
}
catch (Exception e) {
-
e.printStackTrace();
// 打印异常信息
-
}
finally {
-
DBUtil.closeCon();
// 关闭数据库
-
}
-
return resPic;
-
}
-
-
public static void main(String[] args) {
// 主方法
-
System.out.println(DBUtil.delUser(
"aa"));
-
}
-
}
4.软件测试
(1)注册和登陆功能的实现,如图1.5
图1.5注册和登陆功能的实现
(2)登陆成功,如图1.6
图1.6 登陆成功
(3)修改头像,如图1.7
图1.7 修改头像
5.参考材料和文献
6. 总结与体会、意见和建议
通过对人们日常生活细心观察,能很好地发现人们的需求,这就为我们开发一款软件提供了思想。第一步,开发软件首先要做的就是软件分析,从人们的日常需求来确定软件的功能。第二步,确定好了软件的功能后就是要实现具体的功能,为了降低软件开发难度和提高开发进度,可把要实现的功能模块化,然后再一步步实现各功能的细节。第三部,就是软件地测试,测试软件就是找到软件地错误和不足,并加以修改,已达到预期的效果。
个人总结:数据库好坑!
7. 软件使用
软件使用流程为:1、注册2、登陆3、填写信息,保存4、按照编号或者姓名搜索 5、找到联系人后修改图片、保存。
8.小组成员的信息
王争取 TEL:17862810568 Email:[email protected]
张鹏 TEL:17862810957 Email:[email protected]