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","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) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(state != null) {
try {
state.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
public static void main(String [] args) {
jdbctest test = new jdbctest();
test.insertDepartments("研发部", 8);
}
}
老师一直报这个错误,对了很久代码也没有对正确,
