用JDBC向MySQL数据库中插入数据时出错,我插入的是stu2的数据,实际却变成了stu3
studentDao studentDao = new studentDao();
Student stu = new Student(1,"hehe");
Student stu1 = new Student(2,"wuhu");
Student stu2 = new Student(3,"lala");
Student stu3 = new Student(4,"qifei");
//studentDao.addStudent(stu);
studentDao.addStudent(stu2);
addStudent代码
public static boolean addStudent(Student stu){
boolean flag = false;
PreparedStatement pstmt = null;
Connection conn = JDBCUtil.getConnection();
String sql = "insert into students values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,stu.getId());
pstmt.setString(2,stu.getName());
int count = pstmt.executeUpdate();
if(count > 0){
System.out.println("insert succesfuly!");
flag = true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.release(conn,pstmt);
}
return flag;
}
同时在这之前我尝试同时添加stu2和stu3的数据之后报错
[](
```java
//studentDao.addStudent(stu2);
studentDao.addStudent(stu3);
JDBCutil
```java
public class JDBCUtil {
private static String url = "jdbc:mysql://localhost:3306/joe";
private static String username = "root";
private static String password = "123456";
private static Connection conn = null;
static {
try {
conn = DriverManager.getConnection(url, username, password);
if (conn != null){
System.out.println("Connection Succeseful!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet res){
if(res != null){
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
res = null;
}
release(conn,pstmt);
}
public static void release(Connection conn,PreparedStatement pstmt){
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
pstmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
conn = null;
}
}
}