package com.bjsxt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcText {
//向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","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) {
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","root","root");
state = conn.createStatement();
String sql = "update dapartments d set d.department_name = '"+department_name+"',d.location_id = "+location_id+" where d.department_id ="+department_id;
int flag = state.executeUpdate(sql);
System.out.println(flag);
}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) {
JdbcText test = new JdbcText();
// test.insertDepartments("研发部",8);
test.updateDepartments("教学部",6,6);
}
}
老师,我在执行的时候一直报错52行,找了很久实在找不出原因,麻烦帮我看下
