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

📄 example10_7.java

📁 书中的例题
💻 JAVA
字号:
//(1) 客户端程序
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Database_client extends Applet 
implements Runnable,ActionListener
{ Button 查询;
  TextField 商品名称_text,商品价格_text;
  Socket socket=null;
  DataInputStream in=null;
  DataOutputStream out=null;
  Thread thread; 
 public void init()
 {查询=new Button("查询");
  商品名称_text=new TextField(10);
  商品价格_text=new TextField(10);
  add(new Label("输入要查询的商品名称"));
  add(商品名称_text);
  add(new Label("商品价格:"));
  add(商品价格_text);
  add(查询);
  查询.addActionListener(this);
 }
 public void start()
 {
   try{
      socket = new Socket(this.getCodeBase().getHost(), 4331);
      in = new DataInputStream(socket.getInputStream());
      out = new DataOutputStream(socket.getOutputStream());
     } 
   catch (IOException e){System.out.println(e.getMessage());}
   if (thread == null) {
      thread = new Thread(this);
      thread.setPriority(Thread.MIN_PRIORITY);
      thread.start();
     }
 }
 public void stop()
 {
	try{out.writeUTF("客户离开");}
    catch(IOException e1){System.out.println(e1.getMessage());} 
 } 
 public void destroy()
 {
	try{out.writeUTF("客户离开");}
    catch(IOException e1){System.out.println(e1.getMessage());} 
 } 
 public void run()
  { String s=null;
    while(true)
     { try{s=in.readUTF();  }
       catch (IOException e)
          {  商品价格_text.setText("与服务器已断开");
	         break;
          }
       商品价格_text.setText(s);
     }
  }
 public void actionPerformed(ActionEvent e)
 {if (e.getSource()==查询)
     { String s=商品名称_text.getText();
       if(s!=null)		      
        { try{out.writeUTF(s);}
          catch(IOException e1){System.out.println(e1.getMessage());} 
        }               
     }
 }
}

//(2) 服务器端程序:
import java.io.*;import java.net.*;
import java.util.*;import java.sql.*;
public class Database_server 
{   public static void main(String args[])
    {   ServerSocket server=null;
	    Server_thread thread;
         Socket you=null;
         while(true) 
         { try{ server=new ServerSocket(4331);
               }
             catch(IOException e1) {System.out.println("正在监听");} 
             try{ you=server.accept();
                }
             catch (IOException e)
                {System.out.println("正在等待客户");
                }
             if(you!=null) 
                {new Server_thread(you).start();   
                }
             else {continue;}
         }
    }
}
class Server_thread extends Thread
{  Socket socket;
   Connection Con=null;
   Statement Stmt=null;
   DataOutputStream out=null;
   DataInputStream  in=null;
   String s=null;
   String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=TestDB.mdb";
   Server_thread(Socket t)
      { socket=t;
        try {
			  in=new DataInputStream(socket.getInputStream());
              out=new DataOutputStream(socket.getOutputStream());
            }
        catch (IOException e) {System.out.println(e.getMessage());}
        try { 
			  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            }
        catch(ClassNotFoundException e){System.out.println(e.getMessage()); }
        try {
			  Con=DriverManager.getConnection(conURL);
              Stmt=Con.createStatement();
            }
        catch(SQLException ee) {System.out.println(ee.getMessage());}
      }  
   public void run()        
   {  while(true)
       {try{ s=in.readUTF();
             int n=0;
             ResultSet rs=
             Stmt.executeQuery("SELECT * FROM 商品表 WHERE 品名 ="+"'"+s+"'" );
             while (rs.next())
                 { String 英语单词=rs.getString("品名");
                   if(s.equals(英语单词))
                       { out.writeUTF(rs.getString("价格"));
				         n=1;break; 
                       }
                    if(n==0) {out.writeUTF("没有此商品");}
                 }
              sleep(45);
            }
        catch(Exception ee) 
            {
			  System.out.println(ee.getMessage());
			  break;
            }
       } 
   }
}

⌨️ 快捷键说明

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