这里是将GUI,Socket编程,多线程任务,JDBC综合起来,写的一个远程交互小项目。不过这里IP地址是采用127.0.0.1(回环地址)进行测试的,
若访问远程服务器则需要将IP地址进行相应修改。下面是代码分析:
/*
客户端只是进行:查询和注册任务,这里直接发送相应字符串给服务器进行处理即可
1: 建立Socket连接
2:生成相应流对象
3:完成数据的读取或输出
要注意在收发数据时,线程是处于阻塞状态的!
*/
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[])
{
JFrame client = new ConnectFrame();
client.show();
}
}
class ConnectFrame extends JFrame implements ActionListener
{
// 这里客户端没有进行数据库的操作,只是获得服务器返回的信息
private String st1=null;
private String st2=null;
private String st3=null;
private String st4=null;
private String st5=null;
private String stt1=null;
private String stt2=null;
private String stt3=null;
private String stt4=null;
private String stt5=null;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private JLabel user = new JLabel("客户端",SwingConstants.CENTER);
/*
输入向数据库待输入学生的信息,用于扩增数据库
*/
private JLabel namelabel = new JLabel("姓名:",SwingConstants.RIGHT);
private JTextField name = new JTextField(5);
private JLabel numlabel = new JLabel("学号:",SwingConstants.RIGHT);
private JTextField num = new JTextField(10);
private JLabel sexlabel = new JLabel("性别:",SwingConstants.RIGHT);
private JTextField sex = new JTextField(5);
private JLabel gradelabel = new JLabel("平均分:",SwingConstants.RIGHT);
private JTextField grade = new JTextField(5);
private JLabel majorlabel = new JLabel("专业:",SwingConstants.RIGHT);
private JTextField major = new JTextField(10);
private JButton submit = new JButton("注册信息");
/*
输入对数据可操作所需要的信息
*/
private JLabel operation = new JLabel("用户查询数据",SwingConstants.CENTER);
// 这里SwingConstants.CENTER 表示文本在单行输入文本中的初始位置
private JLabel namelabel2 = new JLabel("姓名:",SwingConstants.RIGHT);
private JTextField name2 = new JTextField(5);
private JLabel numlabel2 = new JLabel("学号:",SwingConstants.RIGHT);
private JTextField num2 = new JTextField(10);
private JLabel sexlabel2 = new JLabel("性别:",SwingConstants.RIGHT);
private JTextField sex2 = new JTextField(5);
private JLabel gradelabel2 = new JLabel("平均分:",SwingConstants.RIGHT);
private JTextField grade2 = new JTextField(5);
private JLabel majorlabel2 = new JLabel("专业:",SwingConstants.RIGHT);
private JTextField major2 = new JTextField(10);
// 执行操作的各个按钮
private JButton select = new JButton("查询信息");
/*
用于显示操作的结果
*/
private JLabel resultlabel = new JLabel("查询结果如下",SwingConstants.CENTER);
private JTextArea resultarea = new JTextArea(20,80);
// 初始化各项操作,包括界面布局以及信息传送,接收和数据库操作。
public ConnectFrame()
{


被折叠的 条评论
为什么被折叠?



