⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picsearchpeid.java

📁 完整的JAVA工程
💻 JAVA
字号:
package employee;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.awt.*;
import java.sql.*;

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;

public class PicSearchPeid extends JFrame implements ActionListener{
	Statement stmt;
	ResultSet rs;
	String a="";
	JLabel L1 = new JLabel("职工编号:");
	JLabel L2 = new JLabel("姓名:");
	JLabel L3 = new JLabel("性别:");
	JLabel pic = new JLabel();
	JTextField T1 = new JTextField(10);
	JTextField T2 = new JTextField(10);
	JTextField T3 = new JTextField(5);
	JButton b1=new JButton("下一页");
	JButton b2=new JButton("上一页");
	JButton b3=new JButton("选择照片");
	JButton b4=new JButton("保存照片");
	JFileChooser fc;
	String fname=null;
	
	public PicSearchPeid(){
		JFrame.setDefaultLookAndFeelDecorated(true);
		Font font=new Font("JFrame",Font.PLAIN,14);
		Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
		while(keys.hasMoreElements()){
			Object key=keys.nextElement();
			if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
		}
		if(!ConnectServer.conn("jdbc:odbc:employee_manage","sa","")){
			JOptionPane.showMessageDialog(null,"数据库连接不成功!");
			System.exit(0);
		}
		
		Container con=this.getContentPane();
		con.setLayout(null);
		L1.setBounds(20,20,70,40);//(x,y,长,宽)
		T1.setBounds(90,30,90,20);
		L2.setBounds(20,50,70,20);
		T2.setBounds(90,50,90,20);
		L3.setBounds(20,70,70,20);
		T3.setBounds(90,70,90,20);
		pic.setBounds(200,10,120,150);
		b1.setBounds(90,200,80,30);
		b2.setBounds(180,200,80,30);
		b3.setBounds(30,100,120,30);
		b4.setBounds(30,150,120,30);
		
		pic.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		con.add(L1);
		con.add(T1);
		con.add(L2);
		con.add(T2);
		con.add(L3);
		con.add(T3);
		con.add(pic);
		con.add(b1);
		con.add(b2);
		con.add(b3);
		con.add(b4);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		setTitle("照片的设置与浏览");
		setSize(350,300);
		setVisible(true);
		T1.setEditable(false);
		T2.setEditable(false);
		T3.setEditable(false);
		//设置运行位置,使对话框居中
	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	    this.setLocation( (int) (screenSize.width - 400) / 2 ,
						(int) (screenSize.height - 300) / 2 + 45);
		try{
			String sql="select eid,ename,sex,photo from emppic";
			stmt=ConnectServer.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			rs=stmt.executeQuery(sql);	
			rs.first();			
			loadData();
		}catch(Exception e){}		
	}
	
	void loadData(){
		fname=null;
		try{						
			T1.setText(rs.getString(1));
			T2.setText(rs.getString(2));
			T3.setText(rs.getString(3));
			byte bb[]=rs.getBytes(4);
			ImageIcon icon;
			try{
				icon=new ImageIcon(bb);
			}catch(Exception ee){icon=null;}
			pic.setIcon(icon);			
		}catch(Exception e){e.printStackTrace();}
	}
	
	public void actionPerformed(ActionEvent e){
		try{
			if(e.getSource()==b1){									
			rs.next();
			if(rs.isAfterLast())rs.last();
			loadData();
			}
			else if(e.getSource()==b2){
				rs.previous();
				if(rs.isBeforeFirst())rs.first();
				loadData();
			}
			else if(e.getSource()==b3){//选择其他照片
				fc=new JFileChooser(".");//建立文件选择器
				fc.addChoosableFileFilter(new MyFileFilter("gif"));//设置选择器
				fc.addChoosableFileFilter(new MyFileFilter("jpg"));//设置选择器
				int result=fc.showOpenDialog(this);//打开文件选择器,选择图片文件
				if(result==JFileChooser.APPROVE_OPTION){//若打开
				File file=fc.getSelectedFile();
				fname=file.getAbsoluteFile().toString();
				pic.setIcon(new ImageIcon(fname));
				}
				else if (result==fc.CANCEL_OPTION){//若选择撤销
				     ;//没有选择任何文件,什么也不做
				}
			}
			else if(e.getSource()==b4){//用新照片代替原照片
				if(!fname.equals("")){
					try{
						a=T1.getText();
						String sql="update picture set photo=? where eid=?";
						PreparedStatement pstmt=ConnectServer.con.prepareStatement(sql);
						File file=new File(fname);
						FileInputStream fis=new FileInputStream(file);//建立文件输入流
						pstmt.setBinaryStream(1,fis,(int)file.length());//设置数据流
						pstmt.setString(2,a);
						pstmt.executeUpdate();						
						pstmt.close();
						sql="select eid,ename,sex,photo from emppic";					
						rs=stmt.executeQuery(sql);															
						while(rs.next()){
						    String eid1=rs.getString(1);
							if(eid1.equals(a))break;
						}						
						loadData();						
						
					}catch(Exception ee){ee.printStackTrace();}
					
				}
			}
				
		}catch(Exception ee){ee.printStackTrace();}
	}
}
class MyFileFilter extends FileFilter{
	String ext;
	public MyFileFilter(String ext){
		this.ext=ext;
	}
	public boolean accept(File file){
		if(file.isDirectory()){
			return true;
		}
		String fileName=file.getName();
		int index=fileName.lastIndexOf('.');
		if(index>0&&index<fileName.length()-1){
			String extension=fileName.substring(index+1).toLowerCase();
			if(extension.equals(ext))return true;
		}
		return false;
	}
	//实现getDescription方法,返回描述文件的说明字符串!!
	public String getDescription(){
		return"File(*."+ext+")";
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -