?? 第二十三章例子.txt
字號:
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 + -