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

📄

📁 通过实例可以更好的了解java
💻
📖 第 1 页 / 共 2 页
字号:
public class Database_client extends Applet implements Runnable,ActionListener
{ Button 查询;TextField 英文单词_文本框,汉语解释_文本框;
  Socket socket=null;
  DataInputStream in=null;
  DataOutputStream out=null;
  Thread thread; 
 public void init()
 {查询=new Button("查询");
  英文单词_文本框=new TextField(10);汉语解释_文本框=new TextField(10);
  add(new Label("输入要查询的英文单词"));add(英文单词_文本框);
  add(new Label("汉语解释:"));add(汉语解释_文本框);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){}
  if (thread == null)
   {thread = new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.start();
   }
 }
 public void stop()
 {try{out.writeUTF("客户离开");}
  catch(IOException e1){} 
 } 
 public void destroy()
 {try{out.writeUTF("客户离开");}
  catch(IOException e1){} 
 } 
 public void run()
 {String s=null;
    while(true)
     { try{s=in.readUTF();
          }
        catch (IOException e)
 {汉语解释_文本框.setText("与服务器已断开");break;
}
     汉语解释_文本框.setText(s);
    }
 }
 public void actionPerformed(ActionEvent e)
 {if (e.getSource()==查询)
     { String s=英文单词_文本框.getText();
       if(s!=null)		      
        { try{out.writeUTF(s);}
          catch(IOException e1){} 
        }               
       
     }
 }
}
(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;int n=0;
   String s=null;
    Server_thread(Socket t)
       { socket=t;
         try {in=new DataInputStream(socket.getInputStream());
             out=new DataOutputStream(socket.getOutputStream());
             }
         catch (IOException e)
         {}
         try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
         catch(ClassNotFoundException e){}
         try{
Con=DriverManager.getConnection("jdbc:odbc:moon","gxy","ookk");
              Stmt=Con.createStatement();
             }
         catch(SQLException ee) {}
       }  
  public void run()        
  { while(true)
    { try{s=in.readUTF();
            }
       catch (IOException e) {}
       try {if(!(s.equals("客户离开")))
             {n=0;ResultSet rs=
Stmt.executeQuery("SELECT * FROM 表1 WHERE 单词 ="+"'"+s+"'" );
               while (rs.next())
               {String 英语单词=rs.getString("单词");
                if(s.equals(英语单词))
                  {out.writeUTF(rs.getString("解释")); n=1;break;} 
               }
              if(n==0){out.writeUTF("没有此单词");}
             }
            else if(s.equals("客户离开"))
{Con.close();
System.out.println("客户离开");
Thread.currentThread().yield();break;
             }
           sleep(1);
           }
       catch(InterruptedException e){}
       catch (IOException e)
         {try{Con.close();} catch(SQLException ee) {}
System.out.println("客户离开");
Thread.currentThread().yield();break;
         }
       catch(SQLException ee) {}
    }
  } 
}




例子5
import java.awt.*;import java.net.*;
import java.sql.*;import java.awt.event.*;
class DataWindow extends Frame implements ActionListener
 { TextField englishtext;TextArea chinesetext; Button button;
   DataWindow()
  { super("英汉小词典");
    setBackground(Color.cyan); setBounds(150,150,300,120); 
    setVisible(true);
    englishtext=new TextField(16);chinesetext=new TextArea(5,10);
    button=new Button("确定");Panel p1=new Panel(),p2=new Panel();
    p1.add(new Label("输入要查询的英语单词:"));p1.add(englishtext);
    p2.add(button);
    add(p1,"North");add(p2,"South");add(chinesetext,"Center");
    chinesetext.setBackground(Color.pink);
    button.addActionListener(this);
    addWindowListener(new WindowAdapter()
     {public void windowClosing(WindowEvent e)
     {setVisible(false);System.exit(0);  } } );
  }
 public void actionPerformed(ActionEvent e)
  {if(e.getSource()==button)
    {  chinesetext.setText("查询结果");
       try{ Liststudent();}
       catch(SQLException ee) {}
    }
  }
 public  void Liststudent() throws SQLException
  { String cname,ename;
    try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
     catch(ClassNotFoundException e){}
    Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
    Statement Ex1Stmt=Ex1Con.createStatement();
    ResultSet rs=Ex1Stmt.executeQuery("SELECT *  FROM 表1 ");  
    while (rs.next())
     {ename=rs.getString("单词"); cname=rs.getString("解释");
       if(ename.equals(englishtext.getText()))
       {chinesetext.append('\n'+cname); break;} 
     }
   Ex1Con.close();
if(chinesetext.getText().trim().equals("查询结果"))
 {chinesetext.append('\n'+"没有该单词");  }
  }
}
public class DatabaseTest
{ public static void main(String args[])
    {DataWindow window=new DataWindow();window.pack();
  }
}



例子7
import java.sql.*;import java.awt.*;import java.awt.event.*;
class DataWindow extends Thread
{  Frame f=new Frame("Database");TextArea text=new TextArea();
   String name,xuehao;Date date; int math,physics,english;
   Connection con;Statement sql; ResultSet rs;
   DataWindow()
  {  f.setBounds(150,150,300,120); f.setVisible(true);
    f.add(text,"Center");f.pack();
    f.addWindowListener(new WindowAdapter()
     {public void windowClosing(WindowEvent e)
        { System.exit(0);  } } );
    try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  }
    catch(ClassNotFoundException e){}
    try
      { con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
        sql=con.createStatement();
       }
    catch(SQLException e1) {}
  }
   public void run()
 {  while(true)
     { text.setText(null);
         try{ rs=sql.executeQuery("SELECT * FROM chengjibiao");
               while(rs.next())
              { name=rs.getString(2);  xuehao=rs.getString(1);
                date  =rs.getDate(3);    math=rs.getInt("数学");
                physics=rs.getInt("物理"); english=rs.getInt("英语");
                text.append("姓名:"+name+"\n");text.append("学号:"+xuehao+"\n");  
                text.append("出生:"+date.toString()+"\n");text.append("数学:"+math+"\n");
                text.append("物理:"+physics+"\n");text.append("英语:"+english+"\n");
               }
             }
         catch(SQLException e1) {}
         f.pack();
        try{sleep(2000);}
        catch(InterruptedException exp){}
      }
  }
}
public class E2
{public static void main(String args[])
  { DataWindow w=new DataWindow();w.start();} 
}   






例子7
import java.sql.*;import java.awt.*;import java.awt.event.*;
class DataWindow extends Thread
{  Frame f=new Frame("Database");TextArea text=new TextArea();
   String name,xuehao;Date date; int math,physics,english;
   Connection con;Statement sql; ResultSet rs;
   DataWindow()
  {  f.setBounds(150,150,300,120); f.setVisible(true);
    f.add(text,"Center");f.pack();
    f.addWindowListener(new WindowAdapter()
     {public void windowClosing(WindowEvent e)
        { System.exit(0);  } } );
    try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  }
    catch(ClassNotFoundException e){}
    try
      { con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
        sql=con.createStatement();
       }
    catch(SQLException e1) {}
  }
   public void run()
 {  while(true)
     { text.setText(null);
         try{ rs=sql.executeQuery("SELECT * FROM chengjibiao");
               while(rs.next())
              { name=rs.getString(2);  xuehao=rs.getString(1);
                date  =rs.getDate(3);    math=rs.getInt("数学");
                physics=rs.getInt("物理"); english=rs.getInt("英语");
                text.append("姓名:"+name+"\n");text.append("学号:"+xuehao+"\n");  
                text.append("出生:"+date.toString()+"\n");text.append("数学:"+math+"\n");
                text.append("物理:"+physics+"\n");text.append("英语:"+english+"\n");
               }
             }
         catch(SQLException e1) {}
         f.pack();
        try{sleep(2000);}
        catch(InterruptedException exp){}
      }
  }
}
public class E2
{public static void main(String args[])
  { DataWindow w=new DataWindow();w.start();} 
}   

⌨️ 快捷键说明

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