会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1741楼

问题点:为什么我用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技术(旧) 1742楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1743楼

问题一:在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技术(旧) 1744楼

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

运行报错

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技术(旧) 1745楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1747楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1748楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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