JDBC不适用于数据插入?

时间:2017-06-20 17:56:21

标签: java oracle jdbc

我从JFrame的用户输入中插入数据,该数据由另一个JFrame类调用。我已经检查了Oracle 11g快递版中的查询,它显示没有插入任何数据。任何人都可以通过在此代码中添加main方法来修改它。

class Insert extends JFrame{
    JLabel name,code,address,telephone,email;
    JTextField tname,tcode,ttelephone,temail;
    JButton save,exit;
    JTextArea taddress;
    Insert(){
        //properties of frame
        super("CUSTOMER MASTER");
        setVisible(true);
        setLayout(null);
        setSize(1100,500);
        setResizable(false);
        //Initialisation
        name=new JLabel("Name:");
        code=new JLabel("Code:");
        address=new JLabel("Address:");
        telephone=new JLabel("Telephone:");
        email=new JLabel("E-mail:");
        tname=new JTextField();
        tcode=new JTextField();
        taddress=new JTextArea(4,20);
        ttelephone=new JTextField();
        temail=new JTextField();
        save=new JButton("SAVE");
        exit=new JButton("EXIT");
        //settingBounds
        name.setBounds(10,50,70,25);
        tname.setBounds(90,50,150,25);
        code.setBounds(300,50,70,25);
        tcode.setBounds(380,50,150,25);
        address.setBounds(10,100,70,25);
        taddress.setBounds(90,100,150,75);
        taddress.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        telephone.setBounds(300,100,70,25);
        ttelephone.setBounds(380,100,150,25);
        email.setBounds(10,225,70,25);
        temail.setBounds(90,225,150,25);
        save.setBounds(900,425,100,25);
        exit.setBounds(1000,425,100,25);
        //adding on frame
        add(name);
        add(tname);
        add(code);
        add(tcode);
        add(address);
        add(taddress);
        add(telephone);
        add(ttelephone);
        add(email);
        add(temail);
        add(save);
        add(exit);

        //ActionListener
        save.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
            //getting data from user
            String demail,checkdemail;  
            int dcode,dtelephone;
            String dname=tname.getText();
            String daddress=taddress.getText();
            checkdemail=temail.getText();
            demail=validateEmail(checkdemail);
            try{
            String stelephone=ttelephone.getText();
            dtelephone=Integer.parseInt(stelephone);
            String scode=tcode.getText();
            dcode=Integer.parseInt(scode);
            addData(dname,dcode,daddress,dtelephone,demail);
            }catch(Exception e){
                e.printStackTrace();
            }           
            dispose();
            new CustomerMaster();
            }
        });
        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                dispose();
                new CustomerMaster();
            }
        });
    }
    public void addData(String name,int code,String address,int telephone,String email){
        Connection con;
        String str;
        PreparedStatement ps;

        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","PROJECT","PROJECT");
            str="insert into CUSTOMERMASTER values(?,?,?,?,?)";
            ps=con.prepareStatement(str);
            ps.executeUpdate();
            ps.setString(1,name);
            ps.setInt(2,code);
            ps.setString(3,address);
            ps.setInt(4,telephone);
            ps.setString(5,email);
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        }
    private String validateEmail(String email){
        String vemail=null;
        Pattern p1=Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        Matcher m1=p1.matcher(email);
        Boolean b=m1.matches();
        boolean cb=b;
        if(email.equals("")){
            vemail=email;
        }           
        else{
            if(b==true){
                vemail=email;
            }
            else{
                Object message="Enter Valid Email.";
                JOptionPane.showMessageDialog(null,message);
            }
        }
        return vemail;
    }
}

以下是SQL Developer生成的数据库代码 代码:

CREATE TABLE CUSTOMERMASTER 
(
  NAME VARCHAR2(20 BYTE) NOT NULL 
, CODE NUMBER NOT NULL 
, ADDRESS VARCHAR2(50 BYTE) 
, TELEPHONE NUMBER 
, EMAIL VARCHAR2(20 BYTE) 
) 
LOGGING 
TABLESPACE SYSTEM 
PCTFREE 10 
PCTUSED 40 
INITRANS 1 
STORAGE 
( 
  INITIAL 65536 
  NEXT 1048576 
  MINEXTENTS 1 
  MAXEXTENTS UNLIMITED 
  FREELISTS 1 
  FREELIST GROUPS 1 
  BUFFER_POOL DEFAULT 
) 
NOPARALLEL;

ALTER TABLE CUSTOMERMASTER
ADD CONSTRAINT CUSTOMERMASTER_UK1 UNIQUE 
(
  TELEPHONE 
, EMAIL 
)
USING INDEX 
(
    CREATE UNIQUE INDEX CUSTOMERMASTER_UK1 ON CUSTOMERMASTER (TELEPHONE ASC, EMAIL ASC) 
    LOGGING 
    TABLESPACE SYSTEM 
    PCTFREE 10 
    INITRANS 2 
    STORAGE 
    ( 
      INITIAL 65536 
      NEXT 1048576 
      MINEXTENTS 1 
      MAXEXTENTS UNLIMITED 
      FREELISTS 1 
      FREELIST GROUPS 1 
      BUFFER_POOL DEFAULT 
    ) 
    NOPARALLEL 
)
 ENABLE;

3 个答案:

答案 0 :(得分:0)

假设正确调用了addData方法,则应在设置值后调用ps.executeUpdate();语句,即在代码中的ps.setString(5,email);行之后。

答案 1 :(得分:0)

你的addData()方法应按此顺序排列(我已删除了群集代码)

try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","PROJECT","PROJECT");
            str="insert into CUSTOMERMASTER values(?,?,?,?,?)";
            ps=con.prepareStatement(str);

            ps.setString(1,name);
            ps.setInt(2,code);
            ps.setString(3,address);
            ps.setInt(4,telephone);
            ps.setString(5,email);
            ps.executeUpdate();

            con.commit();     
            ps.close();
            con.close();
        } catch(Exception e){
            e.printStackTrace();
        }

答案 2 :(得分:0)

我发现它没有将值存储在我在获取文本时声明的变量中。在这里,我将介绍问题所在的动作监听器。 的代码

//ActionListener
        save.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
            //getting data from user
            try{
            String demail,checkdemail;  
            int dcode,dtelephone;
            String dname=tname.getText();
            String daddress=taddress.getText();
            checkdemail=temail.getText();
            demail=validateEmail(checkdemail);
            String stelephone=ttelephone.getText();
            dtelephone=Integer.parseInt(stelephone);
            String scode=tcode.getText();
            dcode=Integer.parseInt(scode);
            System.out.println(dname+daddress+demail+dtelephone+dcode);
            addData(dname,dcode,daddress,dtelephone,demail);

            }catch(Exception e){}           
            }
        });