老师,麻烦帮我找找这个bug
package DataBase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test {
public void insertDepartments(String department_name,int location_id) {
Connection conn=null;
Statement state=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建链接,解决乱码问题useUnicode=true&characterEncoding=utf-8
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "root");
String sql="insert into department values('"+department_name+"',"+location_id+")";
//注意不要导错包,使用javasql包下
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
//注意释放statement(先)和connection(后)
} 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();
}
}
}
}
//更新departments表中的department_id为6的数据,将部门修改为教学部,location_id改为6
private void updateDepartments(String departmentString_name,int location_id,int department_id) {
// TODO Auto-generated method stub
Connection conn=null;
Statement state=null;
try {
conn=jdbcUtil.getConnection();
String sql="update department set department.department_name ='"+departmentString_name+"',department.location_id ="+location_id+" where department.location_id ="+department_id;
System.out.println(sql);
//注意不要导错包,使用javasql包下
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
//注意释放statement(先)和connection(后)
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
jdbcUtil.closeResource(state, conn);
}
}
public void selectDepartmentsById(int departmentId) {
Connection conn=null;
Statement state=null;
ResultSet rs=null;
try {
conn=jdbcUtil.getConnection();
state=conn.createStatement();
String sql="select * from department where department.department_id="+departmentId;
//执行查询,返回结果
rs=state.executeQuery(sql);
while (rs.next()) {
rs.getInt("department_id");
rs.getString("department_name");
rs.getInt(2);
System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")+" "+rs.getInt(2));
}
}
catch (Exception e) {
// TODO: handle exception
}finally {
jdbcUtil.closeResource(state, conn,rs);
}
}
public static void main(String[] args) {
test test=new test();
// test.insertDepartments("教学部", 18);
// test.updateDepartments("教学部", 3,2);
test.selectDepartmentsById(1);
}
}
package DataBase;
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() {
// TODO Auto-generated method stub
Connection conn=null;
try {
conn=DriverManager.getConnection(jdbcURL,username,userpassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
private static void closeStatement(Statement state) {
// TODO Auto-generated method stub
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void closeConnection(Connection conn) {
// TODO Auto-generated method stub
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void closeResultSet(ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeResource(Statement state,Connection conn,ResultSet rs) {
closeStatement(state);
closeConnection(conn);
closeResultSet(rs);
}
public static void closeResource(Statement state,Connection conn) {
closeStatement(state);
closeConnection(conn);
}
}
driver="com.mysql.jdbc.Driver";
jdbcURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
username="root";
userpassword="root";

运行增加一条数据代码不会报错,但是查找代码报如上错误