package Frame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import db.DButil;
public class login extends JFrame implements ActionListener{
private JPanel jp=new JPanel();
//创建标签数组
private JLabel[]
jlArray={new JLabel("用户名"), new JLabel("密 码"),new JLabel("")};
private JButton jb1;
private JButton jb2;
private JButton jb3;
private JTextField jtf=new JTextField();
private JPasswordField jpf=new JPasswordField();
//布局构造实现方法
public login(){
Icon icon1=new ImageIcon("login.jpg");
Icon icon2=new ImageIcon("reset.jpg");
Icon icon3=new ImageIcon("zhuce.jpg");
JPanel jp=new JPanel(){
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("bg.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(), icon.getIconHeight(), icon.getImageObserver());
}};
jp.setLayout(null);
jb1=new JButton(icon1);
jb2=new JButton(icon3);
jb3=new JButton(icon2);
jlArray[0].setBounds(70,120,90,32);
jb1.setBounds(365,110,130,60);
jb2.setBounds(365,170,130,60);
jlArray[1].setBounds(70,170,90,32);
//将标签与按钮添加到JPanel容器中
jp.add(jlArray[0]);
jp.add(jlArray[1]);
jp.add(jb1);
jp.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);//为按钮注册动作事件监听器
//添加修改密码的按钮
jb3.setBounds(365,230,130,60);
jp.add(jb3);
jp.add(jtf);
jtf.setBounds(140,120,180,30);//设置文本框位置
jp.add(jpf);
jpf.setBounds(140,170,180,30);//设置密码框位置
jpf.setEchoChar('*'); //密码显示字符形式
jpf.addActionListener(this); //为密码框注册动作事件监听器
this.setVisible(true);//设置窗体的可见性
jpf.addActionListener(this); //为密码框注册动作事件监听器
//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器
jlArray[2].setBounds(70,220,300,30);
jp.add(jlArray[2]);
this.setTitle("登陆");//设置窗体标题
this.setBounds(450,200,500,318);//设置窗体的大小
this.setResizable(false);//设置窗体不让用户调整大小
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);//设置窗体的可见性
this.add(jp);//将窗体添加到面板中
jp.setBackground(Color.blue);
}
public static void main(String[] args) {
new login();//创建登陆窗体
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//得到用户名与密码
String user=jtf.getText().trim();
String pwd=String.valueOf(jpf.getPassword());//返回密码的字符串表示方式
String sql="";
if(e.getSource()==jtf){//事件源为文本框,切换焦点到密码框
jpf.requestFocus();
}
else if(e.getSource()==jb1||e.getSource()==jpf){
//判断用户名和密码是否匹配 查询数据库
if(DButil.check(user, pwd)){
//登陆成功
MainFrame mf=new MainFrame(jtf.getText());//主窗体
this.dispose();
}
else{
//登陆失败
jlArray[2].setText("对不起,非法的用户名和密码");
this.clear();
}
}
else if(e.getSource()==jb2)
{//事件源为注册按钮
if(user.equals("")||pwd.equals(""))
{//如果注册的用户名为空或者密码为空
jlArray[2].setText("用户名和密码都不得为空!!!");
this.clear();//清空输入文本框
}
else
{
sql="select uid from user where uid='"+user+"'";
if(DButil.isExist(sql))
{//用户名已经存在
jlArray[2].setText("对不起,用户名已存在!!!");
this.clear();//清空输入文本框
}
else
{
sql="insert into user values('"+user+"','"+pwd+"')";
if(DButil.update(sql)>0)
{//注册成功
jlArray[2].setText("恭喜您!!!注册成功,请登陆");
}
}
}
}
else if(e.getSource()==jb3)//修改密码的监听
{
//判断是否已经输入用户名和密码
if(user.equals("")||pwd.equals(""))
{
jlArray[2].setText("修改密码先输入正确的用户名和密码!!!");
this.clear();//清空输入文本框
}
//判断是否输入了正确的用户名和密码
else if(DButil.check(user,pwd))
{
//正确的用户名和密码
String password=JOptionPane.showInputDialog(this,"修改密码:","请输入新密码",
JOptionPane.PLAIN_MESSAGE);
//得到新的密码为空
if(password==null||password.equals(""))
{
JOptionPane.showMessageDialog(this,"密码不得为空!!!","错误",
JOptionPane.WARNING_MESSAGE);
}
else
{//密码不为空
sql="update user set pwd='"+password+"' where uid='"+user+"'";//更新密码的SQL
if(DButil.update(sql)>0)
{//密码修改成功
this.clear();//清空输入文本框
jlArray[2].setText("恭喜您!!!密码修改成功,请用新密码登陆");
}
}
}
else
{
JOptionPane.showMessageDialog(this,"用户名或者密码错误!!!","错误",
JOptionPane.WARNING_MESSAGE);
this.clear();//清空输入文本框
}
}
}
package Frame;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import db.DButil;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class MainFrame extends JFrame implements ActionListener,ItemListener {
private String uname=null;//当前用户的名字
private boolean isInsert=false;//是否为添加默认为否
private JPanel topjp=new JPanel();//界面上半部分的JPanel容器
private JButton jba=new JButton("模糊查询");
private JButton jbs=new JButton("查找");
private JTextField jtfs=new JTextField();//按给出信息查找联系人信息
private JRadioButton jrbxm=new JRadioButton("按姓名查找",true);
private JRadioButton jrbbh=new JRadioButton("按学号查找",false);
private ButtonGroup bg=new ButtonGroup();//单选按钮组
private JPanel jpbr=new JPanel();//单选按钮面板
private JPanel jpyInfo=new JPanel();//右侧显示个人信息的面板
private JTextArea jta=new JTextArea(); // 模糊查询得到的信息文本区
private JLabel[] jlInfo={new JLabel("学号:"),new JLabel("姓名:"),
new JLabel("性别:"),new JLabel("出生日期:"),
new JLabel("电话号码:"),new JLabel("QQ:"),
new JLabel("Email:"),new JLabel("地址:"),
new JLabel("添加相片")};
private JButton[] jbInfo={new JButton("编辑"),new JButton("保存"),
new JButton("浏览"), new JButton("上传"),};
private JLabel jlPhoto=new JLabel();//显示图像的JLabel控件
private JTextField[] jtfInfo=new JTextField[10];
private JTextField jtfPhoto=new JTextField();//添加照片到相册的路径
private JFileChooser jfcPic=new JFileChooser();//上传图像的文件选择器
private DefaultTableModel tableModel;
private JTable table;
//性别部分
private JRadioButton jrbMale=new JRadioButton("男",true);
private JRadioButton jrbFemale=new JRadioButton("女");
private ButtonGroup bgGender=new ButtonGroup();
private JPanel jpGender=new JPanel();//单选按钮面板
private JLabel jlDetail=new JLabel();//右侧显示一幅图片的标签
private JSplitPane jspOuter=//上下分割的JSplitPane
new JSplitPane(JSplitPane.VERTICAL_SPLIT,true);
//系统托盘部分
private PopupMenu popup=new PopupMenu();
private SystemTray tray;//定义SystemTray成员变量
private TrayIcon trayIcon;//定义TrayIcon成员变量
private MenuItem exit=new MenuItem("退出程序");//定义菜单
public void initJps()
{//界面上半部分的初始化
topjp.setLayout(null);//设置topjp布局管理器为null
//设置按钮大小并添加到JPanel面板里
jba.setBounds(5,10,120,26);
jba.addActionListener(this);//为模糊查询按钮注册事件监听器
topjp.add(jba);
jbs.setBounds(130,10,80,26);
jbs.addActionListener(this);//为查找按钮注册事件监听器
topjp.add(jbs);//添加按钮到topjp面板里
//设置jtfs文本框大小并添加到jps面板里
jtfs.setBounds(215,10,120,26);
jtfs.addActionListener(this);//为文本框注册事件监听器
topjp.add(jtfs);
//设置单选按钮大小和位置并添加到jpbr面板里同时添加到bg单选按钮组里
jrbxm.setBounds(5,3,50,26);
jrbxm.addItemListener(this);//为单选按钮注册ItemEvent事件监听器
bg.add(jrbxm);
jpbr.add(jrbxm);
jrbbh.setBounds(60,3,50,26);
jrbbh.addItemListener(this);
bg.add(jrbbh);
jpbr.add(jrbbh);
jpbr.setBounds(360,10,200,26);
topjp.add(jpbr);
}
public void initInfo()
{//初始化信息界面
jpyInfo.setLayout(null);//设置布局管理器为空
jpyInfo.setBounds(50,50,380,360);//设置信息面板的大小和位置
jlPhoto.setBounds(220,10,150,170);//设置联系人图像JLabel的大小和位置
jlPhoto.setBorder(BorderFactory.createLineBorder(Color.BLACK));//将JLbel的边框线显现出来
jpyInfo.add(jlPhoto);//将显示联系人照片的JLabel添加到信息面板
tableModel=new DefaultTableModel();
Vector vector1 = new Vector();
DButil db = new DButil();
vector1.add("学号");
vector1.add("姓名");
vector1.add("性别");
vector1.add("出生日期");
vector1.add("电话号码");
vector1.add("QQ");
vector1.add("Email");
vector1.add("地址");
vector1.add("照片路径");
tableModel.setDataVector(null, vector1);
System.out.print(123);
table=new JTable(tableModel);
//table.addMouseListener((MouseListener) this);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane js=new JScrollPane(table);
//添加相片部分的控件
jtfPhoto.setBounds(80,250,200,26);//设置得到照片路径的文本框的大小和位置
jpyInfo.add(jtfPhoto);//将得到照片路径的文本框添加到信息面板
jbInfo[2].setBounds(285,250,80,26);
jbInfo[2].addActionListener(this);//为添加照片的浏览按钮注册事件监听器
jpyInfo.add(jbInfo[2]);
//上传按钮
jbInfo[3].setBounds(380,250,80,26);
jbInfo[3].addActionListener(this);//为添加照片的浏览按钮注册事件监听器
jpyInfo.add(jbInfo[3]);
for(int i=0;i<9;i++)//添加JLabel,并设置大小和位置
{
jlInfo[i].setBounds(20,10+i*30,60,26);
jpyInfo.add(jlInfo[i]);
}
for(int i=0;i<10;i++)
{//初始化一些文本框
jtfInfo[i]=new JTextField();
}
//学号
jtfInfo[0].setBounds(80,10,135,26);
jpyInfo.add(jtfInfo[0]);
//姓名
jtfInfo[1].setBounds(80,40,135,26);
jpyInfo.add(jtfInfo[1]);
//出生日期
jtfInfo[3].setBounds(80,100,135,26);
jpyInfo.add(jtfInfo[3]);
//电话号码
jtfInfo[4].setBounds(80,130,135,26);
jpyInfo.add(jtfInfo[4]);
//QQ
jtfInfo[5].setBounds(80,160,135,26);
jpyInfo.add(jtfInfo[5]);
//Email
jtfInfo[6].setBounds(80,190,135,26);
jpyInfo.add(jtfInfo[6]);
//地址文本框的添加
jtfInfo[7].setBounds(80,220,285,26);
jpyInfo.add(jtfInfo[7]);
//模糊查询信息文本区的添加
js.setBounds(0,350,600,100);
jpyInfo.add(js);
//编辑、保存按钮
jbInfo[0].setBounds(160,300,80,26);
jbInfo[0].addActionListener(this);
jpyInfo.add(jbInfo[0]);
jbInfo[1].setBounds(260,300,80,26);
jbInfo[1].addActionListener(this);
jpyInfo.add(jbInfo[1]);
//性别部分
jrbMale.setBounds(5,3,50,26);
jrbMale.addItemListener(this); //为单选按钮注册ItemEvent事件监听器
bgGender.add(jrbMale);
jpGender.add(jrbMale);
jrbFemale.setBounds(60,3,50,26);
jrbFemale.addItemListener(this); //为单选按钮注册ItemEvent事件监听器
bgGender.add(jrbFemale);
jpGender.add(jrbFemale);
jpGender.setBounds(60,70,125,26);
jpyInfo.add(jpGender);
}
public void clearInfo()//清空信息面板
{
for(int i=0;i<10;i++)
{
jtfInfo[i].setText("");//清空文本框
}
jlPhoto.setIcon(null);//清空图像
}
public void setjtaArea(String information){
if(information.isEmpty()){
JOptionPane.showMessageDialog(this,"所查用户不存在!!!","错误",
JOptionPane.WARNING_MESSAGE);
}
else {
jta.setText(information);
}
}
public void setInfo(Vector<String> pInfo)//将信息向量设置到信息面板中
{//将信息向量按规则填到信息面板里
this.clearInfo();
if(pInfo.size()==0)
{
JOptionPane.showMessageDialog(this,"所查用户不存在!!!","错误",
JOptionPane.WARNING_MESSAGE);
}
else
{
for(int i=0;i<2;i++)
{//显示联系人编号和姓名
jtfInfo[i].setText(pInfo.get(i));
}
if(pInfo.get(2).equals("男"))
{//显示性别
jrbMale.setSelected(true);
}
else
{//显示性别
jrbFemale.setSelected(true);
}
for(int i=3;i<9;i++)
{//显示出生日期、电话号码和QQ
jtfInfo[i].setText(pInfo.get(i));
}
}
}
public Vector<String> getInfo()//从信息面板得到用户输入的信息
{
Vector<String> pInfo=new Vector<String>();
pInfo.add(jtfInfo[0].getText().trim());//添加pid
pInfo.add(jtfInfo[1].getText().trim());//添加pname
String gender=jrbMale.isSelected()?"男":"女";
pInfo.add(gender);//添加性别
pInfo.add(jtfInfo[3].getText().trim());//出生日期
pInfo.add(jtfInfo[4].getText().trim());//电话号码
pInfo.add(jtfInfo[5].getText().trim());//QQ
pInfo.add(jtfInfo[6].getText().trim());//Email
pInfo.add(jtfInfo[7].getText().trim());//地址
String photoPath=jtfPhoto.getText().trim();//得到照片路径
pInfo.add(photoPath);//照片路径
return pInfo;
}
public void monitorSaveButton() //监听保存按钮的方法
{
String sql="";
String pid=jtfInfo[0].getText().trim();//得到联系人的编号
String pname=jtfInfo[1].getText().trim();//得到联系人的姓名
String gender=jrbMale.isSelected()?"男":"女";
String s1=jtfInfo[3].getText().trim();
String s2=jtfInfo[4].getText().trim();
String s3=jtfInfo[5].getText().trim();
String s4=jtfInfo[6].getText().trim();
String s5=jtfInfo[7].getText().trim();
String s6=jtfPhoto.getText().trim();
String sqla="select * from contacts where pid='"+pid+"'";//判断此编号是否存在的SQL
String sqlb="select * from contacts where pname='"+pname+"'";//判断此姓名是否存在的SQL
boolean isIdExist=DButil.isExist(sqla);//得到编号是否存在
boolean isNameExist=DButil.isExist(sqlb);//得到姓名是否存在
if(!(pid.equals(" ")||pname.equals(" ")))
{
if(isIdExist||isNameExist){
JOptionPane.showMessageDialog(this,"联系人已存在"+
" , 添加联系人失败","错误",JOptionPane.WARNING_MESSAGE);
}
else {
sql="insert into contacts values (?,?,?,?,?,?,?,?,?,?)";
String[] paras={pid,pname,gender,s1,s2,s3,s4,s5,s6,null};
if(DButil.update(sql,paras)>0){
JOptionPane.showMessageDialog(this,"联系人保存成功","提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
public void setEditable(boolean Editable)//设置信息窗口是否可编辑
{
jrbFemale.setEnabled(Editable);//设置性别是否可编辑
jrbMale.setEnabled(Editable);//设置性别是否可编辑
for(int i=0;i<8;i++)
{
jtfInfo[i].setEditable(Editable);//设置文本框是否可编辑
}
}
public void monitorSearchButton()//监听查找按钮的方法
{
String name=jtfs.getText().trim();
String sql="";//声明查找字符串
if(name.equals(""))
{
JOptionPane.showMessageDialog(this,"查找条件不能为空!!!",
"错误", JOptionPane.WARNING_MESSAGE);
}
else
{
if(jrbxm.isSelected()==true)
{//按姓名查找
sql="select pid,pname,pgender,pbirthday,pnumber,pQQ,pemail,padress,pphoto from contacts where pname='"+name+"'";
this.setInfo(DButil.getPerInfo(sql));//设置信息面板为该联系人的信息
}
else
{//按编号查找
sql="select pid,pname,pgender,pbirthday,pnumber,pQQ,pemail,padress,pphoto from contacts where pid='"+name+"'";
this.setInfo(DButil.getPerInfo(sql));//设置信息面板为该联系人的信息
}
}
this.setEditable(false);//设置面板不可编辑
}
public void monitorRandomSearchButton(String information){ //监听模糊查询按钮
String adress=jba.getText().trim();
String sql="";//声明查找字符串
if(adress.equals(""))
{
JOptionPane.showMessageDialog(this,"查找条件不能为空!!!",
"错误", JOptionPane.WARNING_MESSAGE);
}
else{
sql="select pid,pname,pgender,pbirthday,pnumber,pQQ,pemail,padress,pphoto from contacts where padress like '%天河区%'";
}
}
public MainFrame(String uname)
{ //主窗体构造器
this.uname=uname;//设置用户名
this.initJps();//界面上半部分的搭建
this.initInfo();
jspOuter.setDividerLocation(46);//设置分割窗体JSplitPane的位置
jspOuter.setTopComponent(topjp);//设置窗体上半部分的控件
jspOuter.setBottomComponent(jpyInfo);//设置下半部分的控件
//设置窗体关闭按钮执行的动作
this.add(jspOuter);
this.addWindowListener(
new WindowAdapter()
{
@SuppressWarnings("deprecation")
public void WindowClosing(WindowEvent e)
{
//将窗体隐藏
MainFrame.this.hide();
}
}
);
//设置主窗体的图标、标题、大小以及可见性
this.setResizable(false);//设置窗体不让调整大小
this.setTitle(uname+"的通讯录");
this.setBounds(420,80,618,550);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbInfo[1])
{//保存按钮的监听
this.monitorSaveButton();
}
else if(e.getSource()==jba)
{//模糊查询按钮的监听
DButil db = new DButil();
Vector vector1 = new Vector();
vector1.add("学号");
vector1.add("姓名");
vector1.add("性别");
vector1.add("出生日期");
vector1.add("电话号码");
vector1.add("QQ");
vector1.add("Email");
vector1.add("地址");
vector1.add("照片路径");
String ad = jtfs.getText().trim();
tableModel.setDataVector(db.getData(ad),vector1);
}
else if(e.getSource()==jbInfo[2])
{//打开图像文件路径
jfcPic.showOpenDialog(this);
if(jfcPic.getSelectedFile()!=null)
{
jtfPhoto.setText(""+jfcPic.getSelectedFile());
}
}
else if(e.getSource()==jbInfo[0])
{//编辑按钮的监听
this.setEditable(true);//设置信息面板可编辑
}
else if(e.getSource()==jbs||e.getSource()==jtfs)
{//查找,按按钮或者在文本框里敲回车
this.monitorSearchButton();
}
}
public void itemStateChanged(ItemEvent e) {
}
public static void main(String[] args){
}
}
package db;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
public class DButil
{
private static String jdbcName="org.gjt.mm.mysql.Driver";//声明驱动类字符串
//声明数据库连接字符串
private static String dbUrl="jdbc:mysql://127.0.0.1:3306/maillist";
private static String dbUserName="root";
private static String dbPassword="admin";
private static Connection con=null;//声明数据库连接对象引用
private static Statement stat=null;//声明语句对象引用
private static PreparedStatement pstmt=null;
private static ResultSet rs=null;//声明结果集对象引用
private static PreparedStatement psInsert=null;//声明预编译语句对象引用
public static Connection getConnection()//得到数据库连接的方法
{
System.out.println("连接中..");
try
{
Class.forName(jdbcName);//加载驱动类
con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);//得到连接
System.out.println("成功连接");
}
catch(Exception e){e.printStackTrace();}
return con;//返回连接
}
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 spwd=rs.getString(1);//得到密码
if(spwd.equals(pwd))
{
flag=true;//密码匹配,登陆成功
}
}
catch(Exception e)
{
flag=false;//有任何异常发生,登陆失败
}
finally{DButil.closeCon();}//关闭数据库连接
return flag;
}
//某条记录是否存在
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;//存在,设置返回值为true
}
}
catch(Exception e)
{
e.printStackTrace();
flag=false;//发生任何异常,置返回结果为false
}
finally{DButil.closeCon();}//关闭数据库连接
return flag;//返回结果
}
public static int update(String sql,String[] paras)//更新数据库
{
int count=0;//声明返回值
try
{
con=DButil.getConnection();
pstmt=con.prepareStatement(sql);
for(int i=0; i<paras.length;i++){
pstmt.setString(i+1, paras[i]);
}
count=pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
count=-1;//更新失败返回值为-1
}
finally{DButil.closeCon();}//关闭数据库连接
return count;//返回结果
}
public static int update(String sql)//更新数据库
{
int count=0;//声明返回值
try
{
con=DButil.getConnection();
stat=con.createStatement();
count=stat.executeUpdate(sql);//执行更新
}
catch(Exception e)
{
e.printStackTrace();
count=-1;//更新失败返回值为-1
}
finally{DButil.closeCon();}//关闭数据库连接
return count;//返回结果
}
//====================添加联系人=================================
public static String insertPerson(String uid,Vector<String> pInfo)
{
String isPathNull="isNotNull";//传过来的图像是不是合法,默认不为空
try{
con=getConnection();//得到数据库连接
if(pInfo.get(8).equals("")||pInfo.get(8)==null)
{//照片路径为空,则不插入图像
psInsert=con.prepareStatement("insert into contacts(pid,pname,pgender,pbirthday,pnumber,"+
"pQQ,pemail,padress,uid)"+
"values(?,?,?,?,?,?,?,?,?)");
}
else
{//照片路径不为空,则插入图像
psInsert=con.prepareStatement("insert into contacts(pid,pname,pgender,pbirthday,pnumber,"+
"pQQ,pemail,padress,pphoto,uid)"+
"values(?,?,?,?,?,?,?,?,?,?,?)" );
File f=new File(pInfo.get(8));//获取选取的图片文件
byte[] b=new byte[(int)f.length()];//创建存储图片数据的数组
FileInputStream fin=new FileInputStream(f);
fin.read(b);fin.close();//读取文件存于byte数组中并关闭输入流
psInsert.setBytes(11,b);//设置pphoto参数的数据
}
for(int i=0;i<8;i++)
{//设置公共信息
psInsert.setString(i+1,pInfo.get(i));
}
psInsert.setString(10,uid);//所属用户
psInsert.execute();psInsert.close();//执行更新并关闭语句
}
catch(FileNotFoundException fnfe){isPathNull="isNull";}//图片路径不对
catch(Exception e){e.printStackTrace();}
finally{DButil.closeCon();}//关闭数据库连接
return isPathNull;
}
public static String updatePerson(String uid,Vector<String> pInfo){
String isPathNull="isNotNull";//传过来的path是不是合法
try{
con=getConnection();
if(pInfo.get(9).equals("")||pInfo.get(9)==null)
{//更新时候,如果照片路径为空,则不更新图像
psInsert=con.prepareStatement("update contacts set pname=?,pgender=?,birthday=?,pnumber=?,"+
"pQQ=?,pemail=?,padress=?,pphoto=?,uid=? where pid='"+pInfo.get(0).trim()+"'");
}
else
{//如果照片路径不为空,则更新图像
psInsert=con.prepareStatement("update contacts set pname=?,pgender=?,page=?,pnumber=?,"+
"pemail=?,pgroup=?,ppostalcode=?,padress=?,uid=?,pphoto=? where pid='"+pInfo.get(0).trim()+"'");
File f=new File(pInfo.get(9));//获取选取的图片文件
byte[] b=new byte[(int)f.length()];//创建存储图片数据的数组
FileInputStream fin=new FileInputStream(f);
fin.read(b);fin.close();//读取文件存于byte数组中并关闭输入流
psInsert.setBytes(10,b);
}
for(int i=1;i<8;i++){//设置公共的信息部分
psInsert.setString(i,pInfo.get(i));
}
psInsert.setString(9,uid);//所属用户
psInsert.execute();psInsert.close();//执行更新并关闭语句
}
catch(FileNotFoundException fnfe){isPathNull="isNull";}//路径不合法
catch(Exception e){e.printStackTrace();}
finally{DButil.closeCon();}//关闭连接
return isPathNull;
}
public Vector getData(String ad){
Vector vector=new Vector();
try {
//陈述对象
con=getConnection();//得到数据库连接
stat=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);//创建语句对象
rs=stat.executeQuery("select * from contacts where padress LIKE '%"+ad+"%'");
ResultSetMetaData rm=rs.getMetaData();
//循环打印出数据库表中数据
int n=rm.getColumnCount();
while(rs.next()){
Vector ve=new Vector();
for(int i=1;i<n+1;i++){
String s=rs.getString(i);
ve.add(s);
}
vector.add(ve);//Vector对象接收表中数据
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{if(rs!=null){
try {
rs.close();//关闭结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat!=null){
try {
stat.close();//关闭陈述对象
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();//关闭连接
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return vector;
}
public static Vector<String> getPerInfo(String sql)//得到联系人信息
{
Vector<String> pInfo=new Vector<String>();
try
{
con=getConnection();//得到数据库连接
stat=con.createStatement();//创建语句对象
rs=stat.executeQuery(sql);//执行查询
while(rs.next())
{
for(int i=1;i<10;i++)
{
System.out.println(rs.getString(i));
pInfo.add(rs.getString(i));//将联系人信息添加到返回向量
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{DButil.closeCon();}//关闭数据库连接
return pInfo;//返回信息集合
}
public static void closeCon()//关闭数据库连接的方法
{
try
{
if(rs!=null){rs.close(); rs=null;}//如果结果集不为空关闭结果集并赋值null
if(stat!=null){stat.close(); stat=null;}//如果语句对象不为空关闭语句对象并赋值null
if(con!=null){con.close(); con=null;}//如果连接不为空关闭连接并赋值null
}
catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args){
DButil DB=new DButil();
System.out.println(DB.getConnection());
}
}
}