问题点:为什么我用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查询:

MySQL查询:

包:
jdbcDemo.rar