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

📄 javacs.java

📁 java做的c/s图书管理系统,用的是socket编程.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.sql.*;
import java.net.*;
import java.io.*;
class client
{
	int port=8000;
	static String host;
	String result="";
	String outstr="";
	Vector vec=new Vector();
	String line="";
	client()
	{
	}
	client(String str)
	{
		host=str;
	}
	client(String str,String sql)
	{
		try
	    {
		  host=str;
		  Socket socket=new Socket(host,port);
		  final Reader reader=new InputStreamReader(socket.getInputStream());
		  final BufferedReader reline=new BufferedReader(reader);
	      PrintWriter toserver=new PrintWriter(socket.getOutputStream());
          Thread t=new Thread(){
			 public void run()
			 {
				  char[]buffer=new char[1024];
				  int char_read;
				  try
				  {
                      while((char_read=reader.read(buffer))!=-1)
				      {
					     for(int i=0;i<char_read;i++)
					     {
						  result+=buffer[i];
						 }

				      }
				      System.out.println(result);
				      line=reline.readLine();
				      while(line!=null)
				      {
						  line=reline.readLine();
						  vec.addElement(line);
					  }
				  }
				  catch(IOException e)
				  {
					  JOptionPane.showMessageDialog(null,"数据读取失败","错误",JOptionPane.ERROR_MESSAGE);
					  System.out.println(e.getMessage());
				  }
			  }
		  };
     	  t.setPriority(Thread.currentThread().getPriority()+1);
		  t.start();
		  outstr=sql;
		  toserver.print(outstr);
		  toserver.flush();

		// socket.close();
	    }
        catch(Exception e)
        {
		  JOptionPane.showMessageDialog(null,"连接失败","错误",JOptionPane.ERROR_MESSAGE);
	      System.out.println(e.getMessage());
        }
   }
   public boolean sendsql(String sql)
   {
	  try
	  {
		  Socket socket=new Socket(host,port);
		  final Reader reader=new InputStreamReader(socket.getInputStream());
		  PrintWriter to_server=new  PrintWriter(socket.getOutputStream());
		  final BufferedReader reline=new BufferedReader(reader);
		  Thread thread=new Thread(){
			  public void run()
			  {
				char []buffer=new char[1024];
				int char_read;
				try
				{
					while((char_read=reader.read(buffer))!=-1)
					{
						for(int i=0;i<char_read;i++)
						{
							result+=buffer[i];
						}
						System.out.println(result);
					}

				}

				catch(IOException e)
				{
					JOptionPane.showMessageDialog(null,"数据读取失败","错误",JOptionPane.ERROR_MESSAGE);
					System.out.println(e.getMessage());

				}
			  }
		  };
		  thread.setPriority(Thread.currentThread().getPriority()+1);
		     thread.start();
		     outstr=sql;
		     to_server.print(outstr);
             to_server.flush();
	  }
	  catch(Exception e)
	  {
		  JOptionPane.showMessageDialog(null,"数据读取失败","错误",JOptionPane.ERROR_MESSAGE);
		  System.out.println(e.getMessage());
		  return false;
	  }
	  return true;
   }
   public boolean getline(String sql)
   {
	  try
	  {
	     Socket socket=new Socket(host,port);
	     final Reader reader=new InputStreamReader(socket.getInputStream());
	     final BufferedReader reline=new BufferedReader(reader);
	     PrintWriter toserver=new PrintWriter(socket.getOutputStream());
	     Thread t=new Thread(){
	     public void run()
	     {
		   try
		   {
	     	    line=reline.readLine();
	     	    while(line!=null)
	    	    {

					if(line.length()>1)
					   vec.addElement(line);
	    		    line=reline.readLine();

	    		   //System.out.println(line);
	    	    }
	       }
	        catch(IOException e)
	   	   {
	   	    JOptionPane.showMessageDialog(null,"数据读取失败","错误",JOptionPane.ERROR_MESSAGE);
	   	    System.out.println(e.getMessage());
	       }
	      }
         };
	        t.setPriority(Thread.currentThread().getPriority()+1);
	   		t.start();
	   		outstr=sql;
	   		toserver.print(outstr);
	   		toserver.flush();
	        //socket.close();
		}
        catch(Exception e)
	    {
			   JOptionPane.showMessageDialog(null,"数据读取失败","错误",JOptionPane.ERROR_MESSAGE);
			   System.out.println(e.getMessage());
			   return false;
	    }
	    return true;

   }
   public void sethost(String ho)
   {
	   host=ho;
   }
   public String getresult()
   {
	   String s=new String(result);
	   result="";
	   return s;
   }
   public Vector getvec()
   {
	   Vector vector=new Vector(vec);
	   vec.removeAllElements();
	   return vector;
   }
}

class MessageBox
{
	MessageBox(String content,String Caption)
	{
		JOptionPane.showMessageDialog(null,content,Caption,JOptionPane.INFORMATION_MESSAGE);
	}
}
class goglevarity
{
	static String str="";
	static boolean isnull=true;
	static int range=1;
	static String usename="";
}
class CPassFrame extends JDialog implements ActionListener
{
	int i=0;
	private boolean pass=false;
	private String username="";
	private String userpass="";
	ImageIcon okimage=new ImageIcon("ct4.gif");
	ImageIcon cancelimage=new ImageIcon("ct1.gif");
	JLabel CPassName=new JLabel("用户名:");
	JLabel CPassWord=new JLabel("用户密码:");
	TextField CTextName=new TextField(20);
	TextField CTextPass=new TextField(10);
	JButton jbtok=new JButton("确定",okimage);
	JButton jbtCancel=new JButton("取消",cancelimage);
	JPanel panel=new JPanel();
	JPanel panel2=new JPanel();
	CPassFrame()
	{
	   CPassName.setHorizontalAlignment(JLabel.CENTER);
	   CPassWord.setHorizontalAlignment(JLabel.CENTER);
	   CTextPass.setEchoChar('*');
	   panel.setLayout(new GridLayout(3,2,3,10));
	   panel.add(CPassName);
	   panel.add(CTextName);
	   panel.add(CPassWord);
	   panel.add(CTextPass);
	   panel.setSize(200,100);
	   panel2.add(jbtok);
	   panel2.add(jbtCancel);
	   jbtok.addActionListener(this);
	   jbtCancel.addActionListener(this);
	   getContentPane().add(panel);
	   getContentPane().add(panel2,BorderLayout.SOUTH);
	   setSize(300,150);
	   setLocation(300,300);
	   setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
		if(e.getSource()==jbtok)
		{
			username=CTextName.getText();
			userpass=CTextPass.getText();
			JudegePass();
			if(!pass)
			{
				i++;

			 }
		}
		if(i==3)
		{
			System.exit(0);
		}
		if(e.getSource()==jbtCancel)
		{
			System.exit(0);
		}
	}
	public boolean getPass()
	{
		return pass;
	}
	public void JudegePass()
	{
		client cl=new client("saiquan-8e62eee");
		cl.sendsql("q"+"01"+"select qx from pass where EmployeeID='"+username+"' and pswo='"+userpass+"'");
		try
		{
		  Thread.currentThread().sleep(100);
	    }
	    catch(Exception ex)
	    {
			System.out.println(ex.getMessage());
		}
		String rs=cl.getresult();
		if(!rs.equals(""))
		{
			goglevarity.range=Integer.parseInt(rs.trim());
			goglevarity.usename=username;
			setVisible(false);
			dispose();
            StartWnd swnd=new StartWnd();
            swnd.setFont(new Font("LucidaTypewriterBold",Font.BOLD,30));
            swnd.startwnd();
			pass=true;
			MainFrame mainframe=new MainFrame();
		    mainframe.setVisible(true);
		    swnd.endwnd();
	     }
	     else
	     {
			 JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误",JOptionPane.INFORMATION_MESSAGE);
		 }

	}


}
class BkImage extends JPanel implements ActionListener
{
	javax.swing.Timer timer=new javax.swing.Timer(1000,this);
	Image image;
	ImageIcon icon=new ImageIcon("wasd.gif");
	Image im=icon.getImage();
	int x=getSize().width;
	int i=0;
	int y=20;
	boolean b=true;
	String str="";
    Calendar cal=new GregorianCalendar();
	int month,day;
	public BkImage(String str)
	{

		image=MygetImage(str).getImage();
		month=cal.get(Calendar.MONTH)+1;
		day=cal.get(Calendar.DAY_OF_MONTH);
        timer.start();

	}
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(image,0,0,getSize().width,getSize().height,this);
		if(b)
		{
		  if(month==1&&day<10)
		  {
				str="新年快乐!";
		  }
		  else if(month==2&&day<16&&day>13)
		  {
			    str="情人节快乐";
		  }
		  else if(month==12&&day<28&&day>15)
		  {
				str="圣诞快乐";
		  }
		  else
		  {
			   str="welcome";
		  }
		  g.drawImage(im,x,y-30,this);
		  g.drawString(str,x-50,y);
		  x-=8;

		  if(x<=0)
		 {
			        x=getSize().width;
					b=false;y+=60;
		 }
		 if(y>getSize().height)
		 {
			 y=20;
		 }
	    }
	}
	public static ImageIcon MygetImage(String str)
	{
		ImageIcon imageicon=new ImageIcon(str);
		return imageicon;
	}
	public void actionPerformed(ActionEvent e)
	{
		if(!b)
		{
			i++;
			if(i>5)
			{
				i=0;
				b=true;
			}
		}
	}
}

class CStatusMessage extends JPanel implements ActionListener
{
	JLabel lb1=new JLabel("        当前操作员:");
	JLabel lb=new JLabel("                                    使用权限:");
	JLabel lb2;
	JLabel lb3=new JLabel("        当前时间:");
	String time="";
	int i=0;
	ImageIcon imic;
	javax.swing.Timer timer=new javax.swing.Timer(1000,this);
	CStatusMessage()
	{
		switch(goglevarity.range)
		{
			case 1:
			   imic=new ImageIcon("1star.gif");
			   break;
		    case 2:
		       imic=new ImageIcon("2star.gif");
			   break;
			case 3:
			   imic=new ImageIcon("3star.gif");
			   break;
			case 4:
			   imic=new ImageIcon("4star.gif");
			   break;

		}
		lb1.setText("        当前操作员:"+goglevarity.usename);
		lb2=new JLabel(imic);
		timer.start();
		add(lb1);
		add(lb);
		add(lb2);
		add(lb3);
		setLayout(new GridLayout(1,3));
    }
	public void actionPerformed(ActionEvent e)
	{
		Calendar calendar=new GregorianCalendar();
		time="";
        time+=calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
        lb3.setText("        当前时间:"+time);
	}
}
class CToolButton extends JPanel implements ActionListener
{
	//String str[]={"SSS","SSS"};
	ImageIcon imageicon1=new ImageIcon("ruku.png");
	ImageIcon imageicon2=new ImageIcon("jieye.gif");
	ImageIcon imageicon3=new ImageIcon("guihuan.png");
	ImageIcon imageicon4=new ImageIcon("xinxi.gif");
	ImageIcon imageicon5=new ImageIcon("xitong.gif");
	JButton jbt1=new JButton("新书入库",imageicon1);
	JButton jbt2=new JButton("图书借阅",imageicon2);
	JButton jbt3=new JButton("图书归还",imageicon3);
	JButton jbt4=new JButton("信息管理",imageicon4);
	JButton jbt5=new JButton("系统管理",imageicon5);
	CToolButton()
	{
		add(jbt1);
		add(jbt2);
		add(jbt3);
		add(jbt4);
		add(jbt5);
		jbt1.addActionListener(this);
		jbt4.addActionListener(this);
		jbt2.addActionListener(this);
		jbt3.addActionListener(this);
		jbt5.addActionListener(this);
		setLayout(new GridLayout(1,5,0,3));

	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jbt1)
		{
			CStoreDlg csdg=new CStoreDlg();
		}
		if(e.getSource()==jbt4)
		{
			 CInfoOperation ciop=new CInfoOperation();

		}
		if(e.getSource()==jbt2)
		{
			CBorrow cbo=new CBorrow();
		}
		if(e.getSource()==jbt3)
		{
			CReturn crt=new CReturn();
		}

⌨️ 快捷键说明

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