吉林大学Java上机实验(Lab4.xls)(含JavaDB安装配置教程)

1. (简答题, 33.3分)

Design a train ticket simulation program Ticket.java. If there are 100 train tickets to be sold at the railway station, now there are 5 ticketing outlets selling tickets at the same time, and 5 threads are used to simulate the ticketing situation at these 5 ticketing outlets. The requirements are as follows:

1Output the ticket number sold at each ticketing outlet

2Train tickets with the same number cannot be sold at the these ticketing outlets.

An example of the output result is shown in the figure below. The number on the left is the ticketing outlet number, and the number on the right is the number of the train ticket sold.

设计了一个火车票仿真程序ticket .java。假设火车站有100张火车票出售,现在有5个售票网点同时售票,用5个线程来模拟这5个售票网点的购票情况。具体要求如下:
(1)输出各票务网点售出的票号;
(2)相同数量的火车票不能在这些售票点出售。
输出结果的示例如下图所示。左边的号码是售票口号码,右边的号码是售出的火车票号码:

5______________100

2______________99

4______________98

1______________97

3______________96

……

4______________3

3______________2

2______________1

Please submit program code and the screenshot of the program output in the answer.


public class ticket_test {

   public static void main(String[] args) {
       ticket x = new ticket();
       //启动线程
       for(int i=1;i<=5;i++) {
           new Thread(x,""+i).start();
       }
   }
}


 public class ticket implements Runnable {
   public int total_tickets=100;
   public int count=100;
   public void run() {

       while(total_tickets>0) {
           try {
               Thread.sleep(101);
               synchronized(this) {
                   if(total_tickets>1) {
                       count--;
                       System.out.println(Thread.currentThread().getName()+"______________"+count);
                       total_tickets--;
                   }
               }
           }catch(Exception e) {
               e.printStackTrace();
           }
       }
   }
}



 

2. (简答题, 33.3分)

Create database “myDB” with JavaDB, and create table “employee” in it as table-1. Then input the corresponding data as table-2.

(1) Display all records of male employees in the “employee” table.

(2) Add a data record to “employee” table: “2017, Xing SF, female, 650”.

(3) Change the “salary” of the record where “sno” is “2017” to “900” and output the result.

使用JavaDB创建数据库“myDB”,并在其中创建表“employee”作为表1。然后输入表2所示的相应数据。
(1)显示“employee”表中所有男性员工的记录。
(2)在“员工”表中增加一条数据记录:“2017,邢顺丰,女,650”。
(3)将“sno”为“2017”的记录中的“salary”改为“900”,输出结果。

 

51052892a4ea4d95b1d0113f283d6c75.png

一、 安装javaDB(Derby)
首先,javaDB环境配置

Apache Derby

1.下载derby数据库放在一个位置中,我把他放在了H:\Java\jdk-19中。
2.配置环境变量。配置’path’变量,H:\Java\jdk-19\db-derby-10.16.1.1-bin\bin是DB目录下的bin目录.

d1e08c60add24078949991343b154c62.png

3.开始—run—cmd,输入sysinfo测试一下,会看到一些Derby信息,这就成功了。

9d21545db45148709e3393ffc6762240.png

4,我们来创建数据库,首先先选择一个路径,用来存放数据库内容的。就存放C:\Users\x\myderby目录下吧,输入 cd C:\Users\x\myderby 回车 这就可以了.然后输入ij.

33954ad0945d2b20e5c8235847815061.png

66bc08f45f81ac51fcc912474041f4d5.png

 

C:\Users\x\myderby>ij

ij 版本 10.16

ij> connect 'jdbc:derby:project;user=root;password=123456;create=true';

ij> connect 'jdbc:derby:project;user=root;password=123456;';

ij(CONNECTION1)> create table employee(sno int primary key,name varchar(8),sex varchar(6),salary float);

ij(CONNECTION1)>  insert into employee values(1001,'Zhang Y','male',675.20);

已插入/更新/删除 1 行

ij(CONNECTION1)>  insert into employee values(1004,'Li X','female',842.00);

已插入/更新/删除 1 行

ij(CONNECTION1)>  insert into employee values(1007,'Wang DS','male',765.00);

已插入/更新/删除 1 行

ij(CONNECTION1)>  insert into employee values(1010,'Zhao YH','female',690.00);

已插入/更新/删除 1 行

ij(CONNECTION1)> select * from employee;

​

ij(CONNECTION1)> select * from employee where sex='male';



ij(CONNECTION1)> insert into employee values(2017,'Xing SF','female',650.00);



ij(CONNECTION1)> update employee set salary=900.00 where sno=2017;



ij(CONNECTION1)> select * from employee;

 

3. (简答题, 33.4分)

Design a client/server communication program based on stream sockets. The requirements are as follows:

(1) Write the server-side program and the client-side program respectively (the port number is 1888).

(2) The client sends information to the server, and the server receives the information and displays it.

(3) At the same time, the server will feedback the information to the client, and the client will receive the information and display it.

Please submit program code and the screenshot of the program output in the answer.

 

设计基于流套接字的客户端/服务器通信程序。要求如下:

(1)分别编写服务端程序和客户端程序(端口号为1888)。

(2)客户端向服务器发送信息,服务器接收信息并显示。

(3)同时,服务器将信息反馈给客户端,客户端接收信息并显示。

请在答案中提交程序代码和程序输出的屏幕截图。

import java.io.*;
import java.net.*;

public class Server {
    private int port = 1888;

    public static void main(String[] args) throws IOException{
        new Server().service();
    }

    public void service() throws IOException{
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server is listening on port " + port);

        Socket socket = serverSocket.accept();
        System.out.println("Client connected: " + socket.getRemoteSocketAddress());

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        String inputLine;
        while ((inputLine = in.readLine()) != null){
            System.out.println("Received from client: " + inputLine);

            // Send data back to the client
            String message = "Server received your message: " + inputLine;
            out.println(message);

            if (inputLine.equals("bye")) break;
        }
        System.out.println("Connection closed!");
        socket.close();
        serverSocket.close();
    }
}
import java.io.*;
import java.net.*;

public class Client {
    private String hostname = "localhost";
    private int port = 1888;

    public static void main(String[] args) throws IOException{
        new Client().connect();
    }

    public void connect() throws IOException{
        Socket socket = new Socket(hostname, port);
        System.out.println("Connected to Server: " + socket.getRemoteSocketAddress());

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));

        String userInput;
        while ((userInput = consoleInput.readLine()) != null){
            out.println(userInput);
            System.out.println("Sent to server: " + userInput);

            String serverResponse = in.readLine();
            System.out.println("Response from server: " + serverResponse);

            if (userInput.equals("bye")) break;
        }

        consoleInput.close();
        in.close();
        out.close();
        socket.close();
    }
}

 

 

 

  • 29
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code Slacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值