.
{ {
String n = t2.getText(); try {
out.writeUTF(n); t2.setText(\);
}
public void actionPerformed(ActionEvent e)
serverSock = new ServerSocket(7777); JScrollPane jsp=new JScrollPane(t1); this.getContentPane().add(jsp,\); JPanel p1=new JPanel(); p1.add(t2); p1.add(b1);
this.getContentPane().add(p1,\); b1.addActionListener(this);
addWindowListener(new WindowAdapter(){ });
setTitle(\服务器\); setSize(340,200); setVisible(true);
try{ sock=serverSock.accept(); //使用accept()阻塞等待客户请求,
//有客户请求到来则产生一个Socket对象,并继续执行 System.out.println(\);
out=new DataOutputStream(sock.getOutputStream());
//由Socket对象得到输入流,并构造相应的DataOutputStream对象 in=new DataInputStream(sock.getInputStream());
out.writeUTF(\你连接服务器成功\); // 给客户反馈信息
Communnion th=new Communnion(this); //建立与客户端交互的线程 th.start(); // 启动线程 public void windowClosing(WindowEvent e){ }
setVisible(false); System.exit(0);
//由socket对象得到输出流,并构造DataInputStream对象
}catch(Exception e){}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
out.writeUTF(\); //退出时告诉客户端
try{
}catch(Exception ee){} dispose(); System.exit(0);
} });
} catch (IOException e1) {
Word资料
.
}
}
// TODO 自动生成 catch 块 e1.printStackTrace();
}
public static void main(String agrs[]) throws IOException
{ Server mainFrame=new Server(); } class Communnion extends Thread { { { {
} try{
fp.out.close(); //关闭socket输出流 fp.in.close(); //关闭socket输入流 fp.sock.close(); //关闭socket fp.serverSock.close(); //关闭ServerSocket }catch(Exception ee){break; }
try{
msg = fp.in.readUTF(); if(msg!=null){ }
fp.t1.append(msg+\);
String msg=null; while(true) }
public void run()
this.fp=fp; Server fp;
Communnion(Server fp)
}catch(Exception ee){}
} }
Word资料
.
三、实验要求:
1、事先预习,写出预习报告 2、上机验证后写出实验报告
实验十六 数据库设计
一、实验目的
1.理解JDBC的功能及体系结构;
2.掌握利用JDBC实现数据库的查询更新等操作的方法; 3.掌握基本的Java数据库程序设计。 二、实验容
案例 成绩查询系统分析
设计一个小程序可以实现各种单科考试、竞赛等成绩的各种查询。将操作与图形用户界面相结合,则很容易实现绩查询系统。程序包括三个类,除了主程序及窗体程序外,数据库连接的功能单独由一个类来完成。程序主界面如图1所示。当输入考号后点击按考生查询按钮,则显示该生成绩如图2所示。
图1 成绩查询系统界面 图2 按照考号查询成
点击“成绩名次”按钮可以查看考试排名如图3所示。点击“未通过”按钮可以查看学生不及格的情况如图4所示。
图3 查询成绩名次 图4 查询未通过
在上面的案例框架中,我们需要利用JDBC连接数据库进行访问。 试尝试实现此功能,补全代码。 1.数据库连接类: import java.sql.*;
public class DbConnect //静态方法提高数据库的连接 {
Word资料
.
} }
2.窗体程序: import java.awt.*; import javax.swing.*; import java.sql.*;
import java.awt.event.*;
public class ScoreInquirementFrame extends JFrame implements ActionListener {
JPanel contentPane; Connection con; Statement st;
JSplitPane jSplitPane1 = new JSplitPane();
BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel1 = new JLabel(\考号\
JButton jButton1 = new JButton(\成绩名次\ JButton jButton2 = new JButton(\未通过\ JTextField jTextField1 = new JTextField();
JButton jButton3 = new JButton(\按考生查询\ //构造方法
public ScoreInquirementFrame() {
}/进行窗口的初始化
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane(); contentPane.setLayout(borderLayout1); setSize(new Dimension(400, 300)); setTitle(\成绩查询系统\ con=DbConnect.getConn(); st=con.createStatement();
jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT); jPanel1.setLayout(null);
jLabel1.setBounds(new Rectangle(24, 15, 49, 25)); jButton1.setBounds(new Rectangle(79, 58, 181, 29));
Word资料