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


package com.bjsxt;

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

public class JdbcTest {
	
	public void insertDepartment(String department_name, int location_id,float salary ){
		Connection con=null;
		Statement str=null;
		
		try {
			// con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test_2?useUnicode=true&characterEncoding=utf-8","root","root");
			con=jdbcUtil.getConnection();
			String sql="insert into department values(default,'"+department_name+"',"+location_id+","+salary+")";
			 str=con.createStatement();
			int flag=str.executeUpdate(sql);
			System.out.println(flag);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcUtil.closeResource(str, con);
			
		}
		
			
		
	}
	
	public static void updateDepartments(String department_name,int location_id,int department_id){
		 Connection con=null;
		 Statement str=null;
		 
		 try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test_2?useUnicode=true&characterEncoding=utf-8","root","root");
			str=con.createStatement();
            String sql="UPDATE department d SET d.department_name='"+department_name+"',d.location_id="+location_id+" WHERE department_id="+department_id+"";
			int flag=str.executeUpdate(sql);
			System.out.println(flag); 
		} catch (ClassNotFoundException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(str!=null){
				try {
					str.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			if(con!=null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		
		
	}
		 
		 
		 
		
		
	}
	
	
	
	public static void main(String[] args) {
		JdbcTest st=new JdbcTest();
		
		//st.insertDepartment("研发部",8,4500);
		//updateDepartments("教学楼",31,2);
		st.insertDepartment("学习部",9,4500);
		
	}
	
	

}




package com.bjsxt;

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

import javax.sql.DataSource;

public class jdbcUtil {
		/*private  static String driver="com.mysql.jdbc.Driver";
		private static String url="jdbc:mysql://localhost/3306/test_2?useUnicode=true&characterEncoding=utf-8";
		private static String username="root";
		private static  String password="root";*/
		private  static String driver;
		private static String url;
		private static String username;
		private static  String password;
		private static DataSource ds;
		static {
			
			ResourceBundle bundle=ResourceBundle.getBundle("jdbc.properties");
			driver=bundle.getString("driver");
			url=bundle.getString("url");
			username=bundle.getString("username");
			password=bundle.getString("password");
			try {	
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
		}
		public static Connection getConnection(){
			
			Connection con=null;;
			try {
				con = DriverManager.getConnection(url, username, password);
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return con;
			
		
			
		}
		
		
		
		
		//关闭 statement;
		public static void closeStatement(Statement state){
			if(state!=null){
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
			}
			
			
		}
		//关闭Connection 
		public static void closeConenection(Connection con){
			if(con!=null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			
			
		}
		
		public static void  closeResource(Statement str,Connection con){
			closeStatement(str);
			closeConenection(con);
			
			
		}
	

}




程序异常如下:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.bjsxt.jdbcUtil
	at com.bjsxt.JdbcTest.insertDepartment(JdbcTest.java:25)
	at com.bjsxt.JdbcTest.main(JdbcTest.java:86)


请问 老师 如何解决 ?

1.PNG





JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 21528楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
        display:none;       隐藏自己,原位置不保留
        visibility:hidden;  隐藏自己,原位置保留
        opacity:0;          隐藏自己,原位置保留
        overflow:hidden;    溢出部分隐藏

     -->
     <style>
         .box{
             width: 200px;
             height: 200px;
             border: 5px solid red;
             /* overflow: hidden;
              */
         }
     </style>
</head>
<body>
   
    <div class="box">
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
        <p>hellohellohellohellohellohellohellohellohello</p>
    </div>
   
</body>
</html>

为什么我这的内容溢出右边了,视频里没有溢出右边

image.png

Python 全系列/第七阶段:网页编程基础/浮动与定位 21529楼
人工智能/第六阶段:机器学习-线性分类/逻辑回归 21530楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql介绍与环境安装 21531楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql的使用 21532楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 21533楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/JAVA入门和背景知识 21536楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/控制语句、方法、递归算法 21539楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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