如何通过swing验证我的数据库条目?

时间:2014-08-18 22:26:10

标签: java mysql swing jdbc

我已经在mySQL数据库中编写了插入信息的代码。 输入数据时,手机号码和电子邮件不能重复。 我可以实现一个ResultSet,但我不知道如何实现多个结果集。

以下是代码:

public void actionPerformed(ActionEvent evt) {

DBcon db = new DBcon();
Connection conn = null;
Statement stmt = null;
ResultSet rs= null;
try{


    Class.forName("com.mysql.jdbc.Driver");

    conn = DriverManager.getConnection(db.DB_URL, db.USER, db.PASS);
    stmt = conn.createStatement();

    rs= stmt.executeQuery("select email from student_info where email= '" + email_txt.getText()+"'" );
    rs = stmt.executeQuery("select mob from student_info where mob= '" + ph_txt.getText()+"'" );   // I'm not getting any error, but here is the problem

    if(rs.next()){
    JOptionPane.showMessageDialog(null,"Email id already Exists");
    email_txt.setText("");
    }

    else if(rs.next()){

        JOptionPane.showMessageDialog(null,"Contact number already Exists");
        ph_txt.setText("");

    }

    {  
    String qry = "Insert into student_info(nam, email, mob, country, city, sex) values(?,?,?,?,?,?)";

    PreparedStatement pst = conn.prepareStatement(qry);
    String comval = country_txt.getSelectedItem().toString();


    pst.setString(1, name_txt.getText());
    pst.setString(2, email_txt.getText());
    pst.setString(3, ph_txt.getText());
    pst.setString(4, comval);   
    pst.setString(5, city_txt.getText());
    pst.setString(6, sex_txt.getText());


    pst.execute();
    JOptionPane.showMessageDialog(null, "Successfully added records");

    rs.close();
    pst.close();
    conn.close();

    }

    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e); 


    }
}
}); 

我们可以使用多个ResultSet来检查重复条目吗?

0 个答案:

没有答案