I have made a simple program, there are two textboxes and one button.
and when i click that button, the program should read the textbox values and enter it to the mysql database.
my workbench mysql connection name is visaCon
username is root
password is ""
database name is userdb and table name is utable.
it has three columns
uid = not null, primary key, auto increment(int)
uname=not null(varchar(100))
upass=not null
(varchar(100))
please give me a solution for this issue?
thanks.
What I have tried:
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
Connection connect = null;
Statement statement = null;
PreparedStatement preSt = null;
ResultSet resultSet = null;
String CntryName1 = jTextField1.getText();
String CntryName2 = jTextField2.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection("jdbc:mysql://userdb","root","");
preSt=connect.prepareStatement("insert into utable values(?,?)");
preSt.setString(2, CntryName1);
preSt.setString(3, CntryName2);
int i=preSt.executeUpdate();
if(i>0){
JOptionPane.showMessageDialog(null, "Data is saved");
}
else{
JOptionPane.showMessageDialog(null, "Data Leaked");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Data Incorrect");
}
}