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 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&useSSL=false","root", "123123"); String sql = "insert into departments values(default,'"+department_name+"',"+location_id+")"; state = conn.createStatement(); int falg = state.executeUpdate(sql); System.out.println(falg); } catch (Exception e) { e.printStackTrace(); }finally { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } //更新departments表中的department_id为6的数据,将部门名称修改为教学部,将location_id修改为6 public void updateDepartments(String department_name,int location_id,int department_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&useSSL=false","root", "123123"); 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 falg = state.executeUpdate(sql); System.out.println(falg); }catch (Exception e){ e.printStackTrace(); }finally { if (state != null){ try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void main(String[] args) { JdbcTest test = new JdbcTest(); //test.insertDepartments("研发部", 8); test.updateDepartments("教学部",6,6); } } |
老师,这个怎么解决?