代码能在mysql connencter8.0上完美运行,但是在5.1.25上,删除功能不能使用
public void del_stu() {
JFrame jf = new JFrame("宿舍信息删除------>");
JLabel jl2 = new JLabel("学生学号:");
JTextField jt2 = new JTextField("",15);
JButton jb1 = new JButton("删除");
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String stuid = jt2.getText();
Class.forName("com.mysql.jdbc.Driver");//加载mysql驱动
String URL = "jdbc:mysql://localhost:3306/dor_information";
Connection connection = Connect.getConnection();
Statement statement = connection.createStatement();
String sql = "delete from dor_stu where '学号' = ?";
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1, stuid);
ptmt.executeUpdate();
ptmt.execute();
ResultSet rs = statement.executeQuery("select *from dor_stu");
while(rs.next()) {
if(!rs.getString(2).equals(stuid)) {
System.out.println("删除成功");
}
}
}catch(Exception e1) {
e1.printStackTrace();
}finally {
try {
Connect.close();
} catch (Exception e2) {
// TODO 自动生成的 catch 块
e2.printStackTrace();
}
}
}
});
jf.setBounds(400, 400, 400, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.add(jl2);
jf.add(jt2);
jf.add(jb1);
}
如果是String sql = "delete from dor_stu where '学号' = ?";的话,会运行,但是没有提示删除成功
如果是String sql = "delete from dor_stu where 学号 = ?";的话,会在sql那里报错,报错内容主要是学号= ?这里,它会报错'??=?'
能够进行数据库删除
没有满足条件吧,所以返回的结果是0
你的删除判断为什么这么写?
删除是否正常直接会返回状态0或1的,没必要去查询对比的啊。
public void del_stu() {
JFrame jf = new JFrame("宿舍信息删除------>");
JLabel jl2 = new JLabel("学生学号:");
JTextField jt2 = new JTextField("",15);
JButton jb1 = new JButton("删除");
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String stuid = jt2.getText();
Class.forName("com.mysql.jdbc.Driver");//加载mysql驱动
String URL = "jdbc:mysql://localhost:3306/dor_information";
Connection connection = Connect.getConnection();
Statement statement = connection.createStatement();
String sql = "delete from dor_stu where '学号' = ?";
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1, stuid);
int result=ptmt.executeUpdate();
if(result==1){
System.out.println("删除成功");
}
}catch(Exception e1) {
e1.printStackTrace();
}finally {
try {
Connect.close();
} catch (Exception e2) {
// TODO 自动生成的 catch 块
e2.printStackTrace();
}
}
}
});
jf.setBounds(400, 400, 400, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.add(jl2);
jf.add(jt2);
jf.add(jb1);
}