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

为什么工具类无法初始化,查了几次代码都找不到原因?

工具类

package com.bjsxt.commns;

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

/**
 * jdbc工具类
 * @author Administrator
 *
 */
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) {
			e.printStackTrace();
		}
	}

	//获取Connection对象
	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;
		
	}
	//关闭Statement
	public static void closeStatement(Statement state){
			try {
				if(state != null){
					state.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	//关闭Connection
	public static void closeConnection(Connection conn){
		try {
			if(conn != null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//关闭资源
	public static void closeResource(Statement state,Connection conn,ResultSet rs){
		closeStatement(state);
		closeConnection(conn);
		closeResultSet(rs);
	}
	//关闭ResultSet连接
	public static void closeResultSet(ResultSet rs){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//事务回滚
	public static void rollback(Connection conn){
		try {
			if(conn != null){
				conn.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

模型层:

package com.bjsxt.pojo;

public class Departments {
	private String departmentName;
	private int location;
	private int departmentId;
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	public int getLocation() {
		return location;
	}
	public void setLocation(int location) {
		this.location = location;
	}
	public Departments() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getDepartmentId() {
		return departmentId;
	}
	public void setDepartmentId(int departmentId) {
		this.departmentId = departmentId;
	}


}

持久层:


接口

package com.bjsxt.dao.impl;

import java.util.List;

import com.bjsxt.pojo.Departments;

public interface DepartmentsDao {
	public List<Departments> selectDeptByName(String deptName);
	public void insertDept(Departments dept);

}

实现类

package com.bjsxt.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bjsxt.commns.JdbcUtil;
import com.bjsxt.pojo.Departments;

public class DepartmentsDaoImpl implements DepartmentsDao{
@Override
public List<Departments> selectDeptByName(String departmentName) {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	List<Departments> list = new ArrayList<>();
	try{
		conn = JdbcUtil.getConnection();
		ps = conn.prepareStatement("select * from departments where department_name=?");
		ps.setString(1, departmentName);
		rs = ps.executeQuery();
		while(rs.next()){
			Departments d = new Departments();
			d.setDepartmentId(rs.getInt("department_id"));
			d.setDepartmentName(rs.getString("departmentName"));
			d.setLocation(rs.getInt("location_id"));
			list.add(d);
		}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		JdbcUtil.closeResource(ps, conn, rs);;
	}
	return list;
}

@Override
public void insertDept(Departments dept) {
	Connection conn = null;
	PreparedStatement ps = null;
	try{
		
		conn = JdbcUtil.getConnection();
		conn.setAutoCommit(false);
		ps = conn.prepareStatement("insert into departemts values(default,?,?)");
		
		ps.setString(1, dept.getDepartmentName());
		ps.setInt(2, dept.getLocation());;
		ps.execute();
		conn.commit();
	}catch(Exception e){
		e.printStackTrace();
		JdbcUtil.rollback(conn);
	}finally{
		JdbcUtil.closeResource(ps, conn, null);
	}
	
}


}

业务层:

接口

package com.bjsxt.server.impl;

import java.util.List;

import com.bjsxt.pojo.Departments;

public interface DepartmentsService {
	public void addDepartments(Departments dept);


}

实现类

package com.bjsxt.server.impl;

import java.util.List;

import com.bjsxt.dao.impl.DepartmentsDao;
import com.bjsxt.dao.impl.DepartmentsDaoImpl;
import com.bjsxt.pojo.Departments;

public class DepartmentsServiceImpl implements DepartmentsService {

	@Override
	public void addDepartments(Departments dept) {
		DepartmentsDao deptDao = new DepartmentsDaoImpl();
		deptDao.insertDept(dept);
		
		
	}



}

测试层

package com.bjsxt.text;

import com.bjsxt.pojo.Departments;
import com.bjsxt.server.impl.DepartmentsService;
import com.bjsxt.server.impl.DepartmentsServiceImpl;

public class Test {

	public static void main(String[] args) {
		Departments dept = new Departments();
		dept.setDepartmentName("数学部2");
		dept.setLocation(20);
		DepartmentsService ds = new DepartmentsServiceImpl();
		ds.addDepartments(dept);
	}

}

运行图片:

image.png

jdbcDemp2.rar


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1757楼

package com.bjsxt;

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

public class JdbcTest {
	
	//向Departments表中添加一条数据
	public void instertDepartments(String department_name,int location_id) {
		Connection conn=null;
		Statement state=null;
		try {
			//驱动注册
			Class.forName("com.mysql.jdbc.Driver");
			//创建链接
			 conn=DriverManager.getConnection("jdbc:musql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root", "root");
			String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
			 state=conn.createStatement();
			int flag=state.executeUpdate(sql);
			System.out.println(flag);
		} 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();
			}
		}
	}	
	}
	public static void main(String[] args) {
		JdbcTest test=new JdbcTest();
		test.instertDepartments("研发部", 8);

	}

}

报错提示:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

java.sql.SQLException: No suitable driver found for jdbc:musql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)

at com.bjsxt.JdbcTest.instertDepartments(JdbcTest.java:18)

at com.bjsxt.JdbcTest.main(JdbcTest.java:47)



我的代码和视频中的一摸一样,怎么报错呢??

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1759楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1760楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1762楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1763楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1764楼

问题点:为什么我用JAVA查询出的数据是相同的而实际在MySQL查询的值是如下图所示

JdbcTest

package cn.bjsxt.jdbc5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.bjsxt.jdbc2.jdbcUtil;

public class JdbcTest {
	public void insertDempartments(String departmentName,int locationId){
	Connection conn = null;
	PreparedStatement pr = null;
	try{
		conn = JdbcUtil.getConnection();
		pr = conn.prepareStatement("insert into departments values(default,?,?)");
		pr.setString(1,departmentName);
		pr.setInt(2, locationId);
		pr.execute();
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		jdbcUtil.closeResource(pr, conn);
	}
	}
	public void updateDempartments(int department_id,String department_name,int location){
		Connection conn = null;
		PreparedStatement pr = null;
		try{
			conn = JdbcUtil.getConnection();
			pr = conn.prepareStatement("update departments set department_name = ?,location_id = ? where department_id = ?");
			pr.setString(1, department_name);
			pr.setInt(2,location);
			pr.setInt(3, department_id);
			pr.execute();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public Departments selectDepartment(int departmentId){
		Connection conn = null;
		PreparedStatement pr = null;
		ResultSet re = null;
		Departments dept = null;
		try{
			conn = JdbcUtil.getConnection();
			pr = conn.prepareStatement("select * from departments where department_id = ?");
			pr.setInt(1, departmentId);
			re=pr.executeQuery();
			while(re.next()){
			dept = new Departments();
			dept.setDepartmentId(re.getInt("department_id"));
			dept.setLocation(re.getInt("location_id"));
			dept.setDepartmentName(re.getString("department_name"));
			}
			}catch(Exception e){
			e.printStackTrace();
			
			
		}finally{
			JdbcUtil.closeResorce(conn, re, pr);
		}
		return dept;
	}
	public List<Departments> selectDepartmentByLikeName(String departmentName){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Departments> list = new ArrayList<>();
		try{
			conn = JdbcUtil.getConnection(); 
			ps = conn.prepareStatement("select * from departments where department_name like ?");
			ps.setString(1, "%"+departmentName+"%");
			rs = ps.executeQuery();
			while(rs.next()){
				Departments dept = new Departments();
				 dept.setDepartmentId(rs.getInt("department_id"));
				 dept.setDepartmentName(rs.getString("department_name"));
				 dept.setLocation(rs.getInt("location_id"));
				 list.add(dept);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResorce(conn, rs, ps);
		}
		return list;
	}
	
	public static void main(String [] args){
		JdbcTest jdbc = new JdbcTest();
		//jdbc.insertDempartments("人力资源部3",10);
		//jdbc.updateDempartments(8, "人力资源部1", 10);
		/*Departments dept = jdbc.selectDepartment(8);
		if(dept!=null){
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocation());
		}*/
		List<Departments> list = jdbc.selectDepartmentByLikeName("人力");
		for(Departments d:list){
			System.out.println(d.getDepartmentId()+" "+d.getDepartmentName()+" "+d.getLocation());
		}
	
	}

}

JdbcUtil:

package cn.bjsxt.jdbc5;

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(){
		Connection conn = null;
		
		try{
		conn = DriverManager.getConnection(jdbcUrl,username,userpassword);	
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	//关闭Statement
	public static void closeStatiemt(Statement state){
		if(state!=null){
			try {
				state.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//关闭Connection
	public static void closeConnection(Connection conn){
		if(conn!=null){
			try{
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	//关闭ResultSet
	public static void closeResultSet(ResultSet re){
		if(re!=null){
			try {
				re.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//关闭资源
	public static void closeResorce(Connection conn,ResultSet re,Statement state){
		JdbcUtil.closeConnection(conn);
		JdbcUtil.closeResultSet(re);
		JdbcUtil.closeStatiemt(state);
		
		
	}
	
}

jdbc.properties:

driver =com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8
username =root
userpassword=123456

Java查询:

image.png

MySQL查询:

image.png

包:

jdbcDemo.rar


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1765楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1766楼

问题一:在sql语句中,为什么int值也需要使用“”拼接?

问题二:反射后并没被使用,无意义啊,不创建反射程序依然进行

代码:

package cn.bjsxt.jdbc;

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



public class JdbcTest {
	//向Departments表中添加一条数据
	public void insertDepartments(String department_name,int location_id ){
		//注册驱动
		Connection conn = null;
		Statement state = null;
		try {
			//Class.forName("com.mysql.jdbc.Driver");
			//创建连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root","123456");
			String sql="insert into departments values(default,'"+department_name+"',"+location_id+")" ;
			state = conn.createStatement();
			int flag = state.executeUpdate(sql);
			System.out.println(flag);
		} 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();
				}
			}
		}
	}
	public void updateDepartments(int department_id,String department_name,int location_id){
		Connection conn = null;
		Statement state = null;
		try{			
			//Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root","123456");
			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){
			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();
				}
			}
		}
	}
	public static void main(String [] args){
		JdbcTest test = new JdbcTest();
		//test.insertDepartments("研发部",8);
		test.updateDepartments(5, "市场部", 5);
	}

}

运行结果:

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1767楼

数据已经添加,但是有报错,查不到报错原因:

运行报错

Mon Jul 15 12:54:15 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

1

代码

package cn.bjsxt.jdbc;

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



public class JdbcTest {
	//向Departments表中添加一条数据
	public void insertDepartments(String department_name,int location_id ){
		//注册驱动
		Connection conn = null;
		Statement state = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//创建连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root","123456");
			String sql="insert into departments values(default,'"+department_name+"',"+location_id+")" ;
			state = conn.createStatement();
			int flag = state.executeUpdate(sql);
			System.out.println(flag);
		} 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();
				}
			}
		}
	}
	public static void main(String [] args){
		JdbcTest test = new JdbcTest();
		test.insertDepartments("研发部",8);
	}

}


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1768楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1770楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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