会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132452个问题
JAVA 全系列/第三阶段:数据库编程/SQL 语言 31876楼
Python 全系列/第一阶段:Python入门/控制语句 31877楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 31880楼
Python 全系列/第一阶段:Python入门/序列 31881楼

package com.itbaizhan.li.InternetCoding;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 一对多的服务器
 */
class Msg extends Thread{
    private Socket socket;
    public Msg(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        this.sendMsg();
    }

    private void sendMsg(){
        BufferedReader br=null;
        PrintWriter pw=null;
        try {
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw=new PrintWriter(socket.getOutputStream());
//这行有疑问  while(true){
                pw.println(br.readLine()+"[ok]");
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(pw!=null){
                pw.close();
            }
        }
    }
}
public class EchoServer {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            System.out.println("服务器已启动,正在监听。。。");
            while(true){
                Socket socket=serverSocket.accept();
                new Msg(socket).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


    在第31行中未加while循环导致客户端发送消息时,服务端返回了如下的结果图一的结果

我的理解是图三中的while循环死循环了,老师为什么在一对多的服务端类中加了while循环后就不会这样了呢?



图一:

image.png


图二:

image.png



图三:

image.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 31882楼

老师好,这个程序里面小写的file到底是什么意思。我的理解里面就是一个变量的名称,为什么在代码中进行一直使用呢(file.getName)就表示文件名,是有什么其他的意思么?(整个程序是没有错误的,正常运行的)下面是我问题的地方和我的代码

WechatIMG3.jpeg

package com.io;

import java.io.*;



public class TestCopy {

    public static void main(String[]args){
        /**File srcFile =new File("/Users/pain_/Downloads/world.docx");
        File targetFile =new File("/Users/pain_/Desktop/world.docx");
        //调用复制文件的方法
        cpoyFile(srcFile,targetFile);
         */
        File srcDir =new File("/Users/pain_/Downloads/xyyyy");
        File targetDir =new File("/Users/pain_/Desktop/xyyyy");
        copyDir(srcDir,targetDir);
    }
    public static void copyDir(File srcDir,File targetDir){
       //判断目的地文件是否存在通过exists()
        if(!targetDir.exists()){
            targetDir.mkdir();//如果目的地不存在,需要使用File类的mkdir()方法进行创建目录
        }
        //list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组
        //listFiles()方法是返回某个目录下所有文件和目录的绝对路径,返回的是File数组
        File[] files = srcDir.listFiles();
        //加强for循环
        //for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体
        for(File file:files) {
            if (file.isFile()) {

            //是文件复制,目录先不管,需要复制srcDir的目录下的指定文件的具体的文件
            copyFile(new File(srcDir + "/" + file.getName()), new File(targetDir + "/" + file.getName()));
        }else{
            //继续调用该方法,使用递归实现
            copyDir(new File(srcDir + "/" + file.getName()), new File(targetDir + "/" + file.getName()));
        }
        }

    }

    public static void copyFile(File srcFile, File targetFile){
        //提高读取效率,从数据源
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            //提高读取效率,写到目的地
            bos = new BufferedOutputStream(new FileOutputStream(targetFile));
            //边读边写
            byte [] buf =new byte[1024];//中转站
            int len =0;//用于存储读到的字节的个数
            while((len=bis.read(buf))!=-1){
                bos.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭
            if(bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 31883楼

//更新departments表中department_id为3的数据,将部门换成教学部,将location_id 换成6
    public void updateDepartments(String department_name,int location_id,int department_id){
        Connection conn=null;
        Statement state=null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT","root","mysql");
            state=conn.createStatement();
            String sql="update departments d set d.department_name='"+department_name+"',d.location_id="+location_id+"where d.department_id="+department_id;
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(state!=null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
    }
    public static void main(String[] args) {
        jdbcTest test=new jdbcTest();
        //test.insertDepartments("研发部",8);
        test.updateDepartments("教学部",6,3);
    }
}

捕获.PNG


老师您好,在更新数据库这节,拼接的更新语句的字符串检查了几遍,没有找到问题,报错

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 31887楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 31889楼
JAVA 全系列/第十六阶段:前后端分离技术VUE/Vue框架 31890楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637