java1996 2016-09-02 06:27 采纳率: 0%
浏览 6094

执行delete没有报错,就是删不掉数据,sql语句也有,求大神

package edu.jmi.xyh.view;

import java.awt.BorderLayout;

public class DeleteView extends JFrame {

private JPanel contentPane;
private JTextField txID;
private JTextField txName;
private JPasswordField txPw;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DeleteView frame = new DeleteView();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DeleteView() {
    setTitle("\u5220\u9664\u5B66\u751F\u4FE1\u606F");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel label = new JLabel("\u5220 \u9664 \u5B66 \u751F \u4FE1 \u606F");
    label.setFont(new Font("宋体", Font.PLAIN, 16));
    label.setBounds(140, 22, 147, 15);
    contentPane.add(label);

    JLabel lblId = new JLabel("\u7528 \u6237 ID\uFF1A");
    lblId.setBounds(95, 55, 69, 15);
    contentPane.add(lblId);

    txID = new JTextField();
    txID.setBounds(174, 52, 113, 21);
    contentPane.add(txID);
    txID.setColumns(10);

    JLabel label_2 = new JLabel("\u7528 \u6237 \u540D\uFF1A");
    label_2.setBounds(95, 98, 66, 15);
    contentPane.add(label_2);

    JLabel label_3 = new JLabel("\u5BC6   \u7801\uFF1A");
    label_3.setBounds(95, 143, 54, 15);
    contentPane.add(label_3);

    txName = new JTextField();
    txName.setBounds(174, 95, 113, 21);
    contentPane.add(txName);
    txName.setColumns(10);

    JButton button = new JButton("\u5220\u9664");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String id=txID.getText();

            String pw=txPw.getText();
            String name=txName.getText();
            Student student=new Student();

            student.setStuId(id);
            student.setStuName(name);
            student.setStuPwd(pw);
            StudentDao studentDao=new StudentDao();
            int result=studentDao.delete(id);
            if(result==0){
                JOptionPane.showMessageDialog(null, "删除成功!");
            }
            else{
                JOptionPane.showMessageDialog(null, "删除失败!");


            }
            System.out.println("id="+id+"pw="+pw);


        }
    });
    button.setBounds(75, 189, 93, 23);
    contentPane.add(button);

    JButton button_1 = new JButton("\u53D6\u6D88");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //生成一个对象
            MainView mainView = new MainView();
            //隐藏窗口
            DeleteView.this.setVisible(false);
            mainView.setVisible(true);
        }
    });
    button_1.setBounds(232, 189, 93, 23);
    contentPane.add(button_1);

    txPw = new JPasswordField();
    txPw.setBounds(174, 140, 113, 21);
    contentPane.add(txPw);
}

public void setMainView(MainView mainView) {
    // TODO Auto-generated method stub

}

public void setID(Object id) {
    // TODO Auto-generated method stub

}

}
2.package edu.jmi.xyh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import edu.jmi.xyh.bean.Student;

public class StudentDao {
//测试用
//ORM object relation mapping hibernate,ibaits
//每一条记录映射每一个对象,记录集合映射成对象集合List
//Dao data access object
//save将一个java对象存储到数据库表中,就是将对象转换为记录

public int save(Student student){
    //建立一个DBOperation,通过这个对象完成数据库的连接操作
    DBOperation dboperation= new DBOperation();
    //获取数据库连接
    Connection connection=null;
    try{
        connection=dboperation.getConnection();
        Statement st=connection.createStatement();
        //字符串拼接
        String sql="insert into student(stuId,stuName,stuPwd)values('"
                +student.getStuId()+"','"+student.getStuName()+"','"+student.getStuPwd()+"')";

        //用来在程序中指挥sql命令执行
        st.execute(sql);        
        System.out.println(sql);
        return 0;
    }catch(Exception ex){
        ex.printStackTrace();
        //返回上层判断
        return -1;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public int update(Student student){
    //建立一个DBOperation,通过这个对象完成数据库的连接操作
    DBOperation dboperation= new DBOperation();
    //获取数据库
    Connection connection=null;
    try{
        connection=dboperation.getConnection();
        Statement st=connection.createStatement();
        //字符串拼接
        String sql="update student set stuName='"+student.getStuName()+
                "', stuPwd='"+student.getStuPwd()+"' where stuId='"+
                 student.getStuId()+"'";
        st.execute(sql);
           System.out.println(sql);
        return 0;
    }catch(Exception ex){
        ex.printStackTrace();
        //返回上层判断
        return -1;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public int delete(Student student){
    //建立一个DBOperation,通过这个对象完成数据库的连接操作
    DBOperation dboperation= new DBOperation();
    //获取数据库连接
    Connection connection=null;
    try{
        connection=dboperation.getConnection();

        String sql="delete from student where stuId='?'";
        //准备执行sql语句
        PreparedStatement pst=connection.prepareStatement(sql);
        //替换上面的问号
        pst.setString(1, student.getStuId());
        pst.execute();
         /* Statement st=connction.createStatement();
           //字符串拼接
           String sql="delete from student where Stuid='"+student.getStuid()+"'";
           st.execute(sql);*/
        //把sql语句打出来
        System.out.println(sql);
        //返回值
        return 0;
        //异常处理
    }catch(Exception ex){
        ex.printStackTrace();
        //返回上层判断
        return -1;
    }finally{
        //关闭与数据库连接
        dboperation.closeConnect(connection);
    }
}
//查询结果放到集合中,集合中的每一个对象都是student <>是泛型
public List<Student> query(){

    //建立一个DBOperation,通过这个对象完成数据库的连接操作
    DBOperation dboperation= new DBOperation();
    //获取数据库
    Connection connection=null;
    List<Student> list = new ArrayList<Student>();
    try{

        connection=dboperation.getConnection();
        Statement st=connection.createStatement();
        //查询语句
        String sql="select* from student";
        //查询语句执行exuecuteQuery,不是execte,执行后放回结果集resultset,把resultsest变成list,通过遍历实现
        ResultSet rs=st.executeQuery(sql);
        //rs.next向下移动一行,刚开始在第0行。如果该行存在,返回true,否则会返回false,
        //每一行对应一个student对象,全部记录对应list
        //getInt,getString,getDate,getBoolean,获取该字段的值
        //getInt(1),getInt("stuId")
        //可能有多条用while,最多用一条用if
        if(rs.next()){
            Student student=new Student();
            String stuId=rs.getString("stuId");
            String stuName=rs.getString("stuName");
            String stuPwd=rs.getString("stuPwd");
            student.setStuId(stuId);
            student.setStuName(stuName);
            student.setStuPwd(stuPwd);
            //最后将student对象存放到集合中
            list.add(student);
        }
        System.out.println(sql);
        return list;
    }catch(Exception ex){
        ex.printStackTrace();
        //返回上层判断
        return null;
    }finally{
        dboperation.closeConnect(connection);
    }
}
//重载
public static Student query(String id) {
    // TODO Auto-generated method stub
    //建立一个DBOperation,通过这个对象完成数据库的连接操作
       DBOperation dbOperation=new DBOperation();
       //获取数据库连接
       Connection connction=null;

       try{
           connction=dbOperation.getConnection();
           Statement st=connction.createStatement();
           //查询语句
           String sql="select * from student where stuid='"+id+"'";
           //查询语句执行exuecuteQuery,不是execute,执行后放回结果集resultset,把resultset变成list,通过遍历实现
           ResultSet rs=st.executeQuery(sql);

           //可能有多条用while,最多一条就用if可以了。码农
           Student student=null;
           if(rs.next()){
               //如果有记录,新建一个student对象,将查到的结果封装到student对象中。
               student=new Student();
               String stuId=rs.getString("stuId");
                String stuName=rs.getString("stuName");
                String stuPwd=rs.getString("stuPwd");
                student.setStuId(stuId);
                student.setStuName(stuName);
                student.setStuPwd(stuPwd);
           }
           return student;
       }catch(Exception ex){
           ex.printStackTrace();
           //返回上层判断
           return null;
       }finally{
           dbOperation.closeConnect(connction); 
       }
}
public static void main(String args[]){
    StudentDao studentDao=new StudentDao();
     List<Student> list=studentDao.query();
       //for each语句 iterator
       for(Student student:list){
           System.out.println(student.getStuName());
       }
    /*Student student=new Student();
    student.setStuId("152118130235");
    student.setStuName("徐耀辉");
    student.setStuPwd("979194874");*/
}
public int delete(String id) {
    // TODO Auto-generated method stub
    return 0;
}

}

  • 写回答

3条回答 默认 最新

  • 糖果给你 2016-09-02 06:37
    关注

    connection.commit();//提交事物
    在catch 异常之前执行上面这个方法

    评论

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL