
I am developing a standalone application which requires me to save the email id and contact number (mobile number) in a database (MySQL). How do i perform a check using JAVA on the data before storing it in the database? PLEASE HELP!!!!!

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class Form extends JFrame
{
JButton ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;;
Form(){
label1 = new JLabel();
label1.setText("Email Id:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("Contact No:");
text2 = new JTextField(20);
ADD=new JButton("Add");
panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(ADD);
add(panel);
setTitle("FORM");
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" );
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from data where email='"+value1+"' and contactNo='"+value2+"'");
int count=0;
while(rs.next()){
count++;
}
if(count>0){
JOptionPane.showMessageDialog(null,"Invalid!Re-enter values!");
text1.setText(" ");
text2.setText(" ");
}
else{
st.executeUpdate("insert into data(email,contactNo) values('"+value1+"','"+value2+"')");
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
}
catch(Exception e){}
}
});
}
}
class FormDemo{
public static void main(String arg[]){
try
{
Form frame=new Form();
frame.pack();
frame.setVisible(true);
}
catch(Exception e)
{
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.