会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134312个问题

添加了在关闭前加个等待时间,还是报错

CLProject.rar

SLProject.rar


package com.bjsxt.serverlogion;

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


public class ServerLogin {

	/**
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 * @throws InterruptedException 
	 * @开发工程师夏青
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		 System.out.println("服务器已经启动");
          ServerSocket server=new ServerSocket(6666);
         
          Socket socket=server.accept();
          
          ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
          User user=(User)ois.readObject();

          System.out.println(socket.getInetAddress().getHostName()+"请求登录:用户名:"+user.getUserName()+"\t 密码:"+user.getPassWord());
          String str="";
          if("bjsxt".equals(user.getUserName())&&"bjsxt".equals(user.getPassWord())){
        	  str="登录 成功";
          }
          else{
        	  str="用户名或密码输入错误";
          }
          DataOutputStream dos=new DataOutputStream (socket.getOutputStream());
          dos.writeUTF(str);
          if(dos!=null){
        	  try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	  dos.close();
          }
          if(ois!=null){
        	  try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	  ois.close();
          }
          if(socket!=null){
        	  try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	  socket.close();
          }
	}

}


image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 29836楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 29837楼

问题一:Server运行,游览器无响应啊?

问题二:StringBuilder sb,添加了那么多字符串的意义在哪里,响应只显示“成功”,其余的是被游览器解析的吗?

package cn.bjsxt.server;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {//服务器,启动和停止服务
	private ServerSocket server;
	public static void main(String[] args){
		Server server = new Server();//创建服务器对象
		server.start();
	}
	public void start(int port){
		try {
			server = new ServerSocket(port);
			this.receive();//调用接收请求信息的方法
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void start(){
		this.start(8888);
	}
	private void receive() {
		
		try {
			//(1)监听
			Socket client = server.accept();
			//获取用户的请求
			/*InputStream is = client.getInputStream();
			byte [] buf = new byte[20480];
			int len = is.read(buf);
			System.out.println(new String(buf,0,len));*/
			Request req = new Request(client.getInputStream());
			//req.show();
			/**做出响应*/
			StringBuilder sb = new StringBuilder();
			sb.append("HTTP/1.1").append(" ").append(200).append(" ").append("OK").append("\r\n");
			sb.append("Content-Type:text/html;charset=utf-8").append("\r\n");
			//内容
			String str="<html><head><title>响应结果</ritle></head><body>成功</body></html>";
			sb.append("Content-Length:"+str.getBytes("utf-8").length).append("/r/n");
			sb.append("\r\n");
			sb.append(str);
			//通过输出流发送出去
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void stop(){
		
	}

}

image.png

http_server4.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 29838楼
JAVA 全系列/第十三阶段:高性能数据处理、NoSQL、分库分表/Redis 29839楼

flask_shop.zip

from flask_shop.user import user  #user的蓝图
from flask_shop.user import models
@user.route("/")
def index():
    return "User Hello"

老师 我想问下 关于数据库这个models 为什么要放在view里 为什么不能放在其他的里边

Python全系列/第九阶段:Flask百战电商后台系统/Flask百战电商后台项目 29842楼
WEB前端全系列/第五阶段:前后端交互/网络请求AJAX 29843楼
Python全系列/第十三阶段:高并发性能怪兽-Tornado 框架/Tornado 深入学习(旧) 29844楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于SpringSecurity实现后台登录功能 29845楼
Python全系列/第一阶段:AI驱动的Python编程/序列 29846楼
人工智能/第二阶段:人工智能基础-Python基础/Python基础语法-旧 29847楼

image.png




image.png

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;


public class JdbcUtil {
	private static String driver ;
	private static String jdbcUrl;
	private static String username;
	private static String userpassword;
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
		bundle.getString("driver");
		bundle.getString("jdbcUrl");
		bundle.getString("username");
		bundle.getString("userpassword");
		try {
			
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		Connection conn=null;
		try {
			conn=	DriverManager.getConnection(jdbcUrl, username, userpassword);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
	
	public static void closeStatement(Statement state){
		
			try {
				if(state != null){
					state.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public static void closeConnection(Connection conn){
		
			try {
				if(conn != null){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	public static void closeResource(Statement state,Connection conn){
		closeStatement(state);
		closeConnection(conn);
	}
	
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JdbcTest {

	public void updateDempartments(String department_name,int location_id,int department_id){
		Connection conn = null;
		Statement state = null;
		try {
			conn=JdbcUtil.getConnection();
			String sql="update departments d set d.department_name='"+department_name+"',d.location_id="+location_id+" where d.department_id="+department_id;			
			state = conn.createStatement();
			int flag=	state.executeUpdate(sql);
			System.out.println(flag);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(state, conn);
		}
	}
	public static void main(String args[]){
		JdbcTest dbd = new JdbcTest();
		dbd.updateDempartments("研发部",5,5);
	}
}

老师,包也导了,为何还报上面的错,麻烦看一下

JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 29848楼
Python全系列/第八阶段:Web全栈开发基础与前端/CSS3 29849楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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