会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132437个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 4262楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 4263楼
JAVA 全系列/(隐藏)第二十三阶段:数字货币交易所项目/IASS基础服务的搭建和开发 4264楼
JAVA 全系列/第七阶段:生产环境部署与协同开发/Docker 4265楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 4266楼

老师,麻烦帮我找找这个bug

package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class test {
	public void insertDepartments(String department_name,int location_id) {
		Connection conn=null;
		Statement state=null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//创建链接,解决乱码问题useUnicode=true&characterEncoding=utf-8
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "root");
			String sql="insert into department values('"+department_name+"',"+location_id+")";
			//注意不要导错包,使用javasql包下
			state=conn.createStatement();
			int flag=state.executeUpdate(sql);
			System.out.println(flag);
			//注意释放statement(先)和connection(后)
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(state!=null) {
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	//更新departments表中的department_id为6的数据,将部门修改为教学部,location_id改为6
	private void updateDepartments(String departmentString_name,int location_id,int department_id) {
		// TODO Auto-generated method stub
		Connection conn=null;
		Statement state=null;
		try {
			conn=jdbcUtil.getConnection();
			String sql="update department set department.department_name ='"+departmentString_name+"',department.location_id ="+location_id+" where department.location_id ="+department_id;
			System.out.println(sql);
			//注意不要导错包,使用javasql包下
			state=conn.createStatement();
			int flag=state.executeUpdate(sql);
			System.out.println(flag);
			//注意释放statement(先)和connection(后)
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jdbcUtil.closeResource(state, conn);
		}
	}
	public void selectDepartmentsById(int departmentId) {
		Connection conn=null;
		Statement state=null;
		ResultSet rs=null;
		try {
			conn=jdbcUtil.getConnection();
			state=conn.createStatement();
			String sql="select * from department where department.department_id="+departmentId;
			//执行查询,返回结果
			rs=state.executeQuery(sql);
			while (rs.next()) {
				rs.getInt("department_id");
				rs.getString("department_name");
				rs.getInt(2);
				System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")+" "+rs.getInt(2));
			}
		} 
		catch (Exception e) {
			// TODO: handle exception
		}finally {
			jdbcUtil.closeResource(state, conn,rs);
		}
	}
	public static void main(String[] args) {
		test test=new test();
//		test.insertDepartments("教学部", 18);
//		test.updateDepartments("教学部", 3,2);
		test.selectDepartmentsById(1);
	}
}
package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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 {
		//读取Properties文件
		ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
		driver=bundle.getString("driver");
		jdbcURL=bundle.getString("jdbcURL");
		username=bundle.getString("username");
		userpassword=bundle.getString("userpassword");
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取Connection对象
	public static Connection getConnection() {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(jdbcURL,username,userpassword);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	private static void closeStatement(Statement state) {
		// TODO Auto-generated method stub
		if(state!=null) {
			try {
				state.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private static void closeConnection(Connection conn) {
		// TODO Auto-generated method stub
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private static void closeResultSet(ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void closeResource(Statement state,Connection conn,ResultSet rs) {
		closeStatement(state);
		closeConnection(conn);
		closeResultSet(rs);
	}
	public static void closeResource(Statement state,Connection conn) {
		closeStatement(state);
		closeConnection(conn);
	}
}
driver="com.mysql.jdbc.Driver";
jdbcURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
username="root";
userpassword="root";

image.png

运行增加一条数据代码不会报错,但是查找代码报如上错误

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 4268楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 4270楼

老师,能帮我检查一下哪里出错了吗,在左上角没有字体显示出来

import pygame
SCREEN_WIDTH=700
SCREEN_HEIGHT=500
BG_COLOR=pygame.Color(0,0,0)
TEXT_COLOR=pygame.Color(255,0,0)
class MainGame():
    window=None #类属性window
    def __init__(self):
        pass
    #开始游戏
    def startGame(self):
        #加载主窗口
        pygame.display.init()
        #设置窗口的大小及显示,MaiGame.window是对window属性的赋值
        MainGame.window=pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
        #设置窗口标题
        pygame.display.set_caption("坦克大战1.03")
        #让窗口一直显示,wile True循环调用pygame.display.update()方法
        while True:
            #给窗口设置填充色
            MainGame.window.fill(BG_COLOR)
            pygame.display.update()
            #获取事件
            self.getEvent()
            #绘制文字
            MainGame.window.blit(self.getTextSuface("敌方坦克数量剩余数量:{}".format(6)),(10,10))
    #结束游戏
    def endGame(self):
        print("谢谢使用")
        exit()
    #左上角文字的绘制
    def getTextSuface(self,text):
        #初始化字体模块
        pygame.font.init()
        #查看所有字体名称
        #print(pygame.font.get_fonts())
        #获取字体Font对象
        font=pygame.font.SysFont("kaiti",18)
        textSurface=font.render(text,True,TEXT_COLOR)
        return textSurface

    #获取事件
    def getEvent(self):
        eventlist=pygame.event.get()#调用pygame中的event方法,返回的是事件列表
        #遍历事件
        for event in eventlist:
            #判断按下的键是关闭还是键盘按下
            #如果按下的是退出,关闭窗口
            if event.type==pygame.QUIT:
                self.endGame()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_LEFT:
                    print("按下左键,坦克向左移动")
                elif event.key==pygame.K_RIGHT:
                    print("按下右键,坦克向右移动")
                elif event.key==pygame.K_UP:
                    print("按下上键,坦克向上移动")
                elif event.key==pygame.K_DOWN:
                    print("按下下键,坦克向下移动")


if __name__=="__main__":
    MainGame().startGame()
    #MainGame().getTextSuface()


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 4272楼
Python 全系列/第十五阶段:Python 爬虫开发/动态数据抓取 4273楼
JAVA 全系列/第八阶段:SpringBoot与MybatisPlus/Spring Boot旧 4274楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 4275楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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