会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132463个问题
JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/Swagger 31261楼
Python 全系列/第一阶段:Python入门/控制语句 31265楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 31268楼


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 全系列/第三阶段:数据库编程/JDBC技术(旧) 31271楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 31273楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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