Hello,
I have one method for checking fields entered, if they are consistent enough then I am inserting into the database. Another on to check duplicates:
//check fields and if alright insert in table
public void checkFields(String a, String b, String c, String d){
if(a.length() >= 4 && b.length() >= 4 && c.matches("\\d+") && d.length() >= 4){
goodLength = true;
}
if( goodLength) {
System.out.println("You entered a good password!");
String e = cb5.getSelectedItem().toString();
String f = cb6.getSelectedItem().toString();
String g = cb7.getSelectedItem().toString();
Properties conProps = new Properties();
conProps.setProperty("user", "root");
conProps.setProperty("password", "root");
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock", conProps);
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
} catch (SQLException ex) {
Logger.getLogger(NewProduct.class.getName()).log(Level.SEVERE, null, ex);
}
String sql = ("INSERT INTO Products(Name, Description, Quantity,Price, Location, Category, Supplier, Notes, Image, Class1, Class2) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
try {
con.setAutoCommit(false);
st = (PreparedStatement) con.prepareStatement(sql);
st.setString(1, t1.getText());
st.setString(2, t2.getText());
st.setString(3, t3.getText());
st.setString(4, t4.getText());
st.setString(5, e);
st.setString(6, f);
st.setString(7, g);
st.setString(8, t12.getText());
st.setBytes(9, person_image);
st.setString(10, txt.getText());
st.setString(11, txt2.getText());
st.execute();
st.close();
con.commit();
JOptionPane.showMessageDialog(null, "saved!");
} catch (Exception se) {
se.printStackTrace();
}
updateTable();
displayImage(t3.getText());
} else if(!goodLength) {
JOptionPane.showMessageDialog(null, "Field have to be at least 5 Characters, make sure you enter a correct number in Quantity");
}
}
My other method is checkDuplicates of the first field only
public void checkDuplicates(String pass) {
ResultSet rs = null;
Properties conProps = new Properties();
conProps.setProperty("user", "root");
conProps.setProperty("password", "root");
try {
con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/stock", conProps);
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE );
} catch (SQLException ex) {
}
String sql = ("select * from Products where Name = '" + pass + "'");
try {
con.setAutoCommit(false);
st = (PreparedStatement) con.prepareStatement(sql);
// st.setString(1,pass);
st.executeQuery();
rs = st.getResultSet();
if(rs.next()){
System.out.print("Exist");
JOptionPane.showMessageDialog(null, "Product Already Exist!");
} else{
checkFields(t1.getText(), t2.getText(), t3.getText(),t4.getText());
}
st.close();
con.commit();
}
catch (Exception e) {
System.out.println(e);
}
//notifyListener();
}
It does not work, I do not get any error message but I have tested each method differently. They work fine, however when I try to have them like that it does not work.
When I enter a duplicate field I get the error message, when I enter a unique field that is not in the database I get you have entered a good password but not the save part.