第四次考核

1.码云地址:https://gitee.com/ztsxxny/codes

2.服务器端:

import java.util.*;

import java.io.*;

import java.net.*;

public class Server_Main

{

    public static void main(String args[])

    {

    Scanner scanner = new Scanner(System.in);

    ServerSocket server = null;

    ServerThread thread;

    Socket kehu = null;

    while (true){

    try {

    server = new ServerSocket(2010);

    }

    catch(IOException e1){

        System.out.println("正在监听");

    }

    try {

        System.out.println("等待呼叫");

        kehu = server.accept();

    }

    catch (Exception e)

    {

        System.out.println("正在等待客户"+e);

    }

    if (kehu !=null){

    new ServerThread(kehu).start(); }

   }

  }

}

class ServerThread extends Thread{

    Scanner scanner = new Scanner(System.in);

    Socket socket;

    DataOutputStream out=null;

    DataInputStream in=null;

    String s = null;

    ServerThread(Socket t)

    {

        socket = t;

        try

        {

            out = new DataOutputStream(socket.getOutputStream());

            in = new DataInputStream(socket.getInputStream());

            for(int i=0;i<10;i++)

        {

            String s=in.readUTF(); 

            System.out.println("客户来话:"+s);

            out.writeUTF(scanner.nextLine());

         

        }

        }

        catch (IOException e){}

    }

     

}

3.客户端:

import java.io.*;

import java.net.*;

import java.util.*;

public class Client_Main

{

    public static void main (String args[])

    {

        Socket mysocket;

        DataInputStream in=null;

        DataOutputStream out=null;

        try

        {

            mysocket = new Socket("127.0.0.1",2010);

            in = new DataInputStream(mysocket.getInputStream());

            out = new DataOutputStream(mysocket.getOutputStream());

            for (int i=0;i<10;i++)

            {

                Scanner scanner = new Scanner(System.in);    

                String source = scanner.nextLine();

                out.writeUTF(source);

                String s= in.readUTF();

                System.out.println("收到服务器的回答:"+s);

                Thread.sleep(500);

            }

        }

        catch(Exception e)

        {

            System.out.println("服务器已断开"+e);

        }

    }

}

4.数据库:

Connection con=null;

Statement sql;

ResultSet rs;

try{ Class.forName("com.mysql.kdbc.Driver");

}

catch(Exception e){}

String uri="jdbc:mysql://localhost:3306/client?useSSL=true";

String user="root";

String password="111111";

try{con = DriverManager.getConnection(uri,user,password);}

catch(SQLException e){}

String jilu="";

String sqlStr = ""+jilu;

try{

sql=con.createStatement();

rs=sql.executeQuery("SELECT bir FROM mess WHERE Number=1001");

while(rs.next()){

String number=rs.getString(1);

//String name=rs.getString(2);

//Date date=rs.getDate(3);

//float height=rs.getFloat(4);

System.out.print(number);

//System.out.print(name);

//System.out.print(date);

//System.out.print(height);

}

con.close();

 

}

catch(SQLException e){

System.out.println(e);

}

5.解题思路:参考书上的世界杯通信,经过改编,然而还是有问题。

6.遇到的问题:本次考核是将通信与数据库结合到一起,单个的例子书上都有,但是并没有解决过合在一起的,经过很多实验还是没有成功。

7总结:不能够灵活的运用所学到的知识,对知识的理解有欠缺,缺乏练习,今后会多加练习,尽量的做到对知识的理解。

学习内容代码行数博客字数
输入输出流 150行---
多线程机制100行---
数据库40---
总计300150

 

转载于:https://www.cnblogs.com/ztsxxny/p/10124327.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值