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


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技术(旧) 1097楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1098楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1099楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1101楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 1102楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1103楼

package com.itbaizhan;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * blob类型操作测试类
 */
public class BlobTest {
    /**
     * 向Movie表中插入数据
     */
    public void insertMovie(String moviename, InputStream is){
        Connection connection = null;
        PreparedStatement ps = null;

        try{
            //获取连接
            connection = jdbcUtils.getConnection();
            //获取PreparedStatement对象
            ps = connection.prepareStatement("insert into movie values(default,?,?)");
            //绑定参数
            ps.setString(1,moviename);
            ps.setBlob(2,is);
            ps.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtils.closeResource(ps,connection);
        }

    }

    public static void main(String[] args) throws FileNotFoundException {
        BlobTest bt = new BlobTest();
        //创建读取文件的IO流
        InputStream is = new FileInputStream(new File("d:/1.jpg"));
        bt.insertMovie("战狼",is);
    }
}

image.png为什么我这样打出来会显示不了名字

JAVA 全系列/第三阶段:数据库编程/JDBC技术 1105楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1106楼

MySQL的安装问题。

win10系统,64位,安装5.7.36版本。

再下面这幅图的一步卡住啦。(自己百度网上各种方式,还是会这样)。

image.png

log里的内容如下:

Saving my.ini configuration file...

Saved my.ini configuration file.

Ended configuration step: Writing configuration file


Beginning configuration step: Updating Windows Firewall rules


Adding a Windows Firewall rule for MySQL57 on port 3306.

Attempting to add a Windows Firewall rule with command: netsh.exe advfirewall firewall add rule name="Port 3306" protocol=TCP localport=3306 dir=in action=allow

确定。



Successfully added the Windows Firewall rule.

Ended configuration step: Updating Windows Firewall rules


Beginning configuration step: Adjusting Windows service


Attempting to grant Network Service require filesystem permissions.

Granted permissions.

Adding new service

New service added

Ended configuration step: Adjusting Windows service


Beginning configuration step: Initializing database (may take a long time)


Deleting the data directory from a previous (failed) configuration...

Attempting to run MySQL Server with --initialize-insecure option...

Starting process for MySQL Server 5.7.36...

Starting process with command: C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqld.exe --defaults-file="C:\ProgramData\MySQL\MySQL Server 5.7\my.ini" --console --initialize-insecure=on --lower-case-table-names=1...

2022-05-26T07:06:52.121753Z 0 [ERROR] Invalid value for named_pipe_full_access_group.

2022-05-26T07:06:52.121817Z 0 [ERROR] Aborting

Process for mysqld, with ID 12228, was run successfully and exited with code 1.

Failed to start process for MySQL Server 5.7.36.

Database initialization failed.

Ended configuration step: Initializing database (may take a long time)


JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1109楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1110楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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