(මේ ජවා ටිකේ රතුපාටින් තියෙන ටික මම Add කරා Duplicate නවත්තන්න. ඒත් ඒ ටික වැඩ නෑ. Error එකක් එන්නෙත් නෑ හැබැයි. මේකේ වැරැද්ද හොයා ගන්න පුලුවන් නම් කියන්න වැඩ්ඩෙක්.
MySQL Table Name - category
MySQL Table Field Names - id, category, status
Text fields on JFrame - txtcat , status(combobox) )
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Add a category , add button
String category = txtcat.getText();
String status=txtstatus.getSelectedItem().toString();
//Duplicate check - not work
String dup = "SELECT category FROM category where category = '"+category+"'";
if(dup.equals(category)){
JOptionPane.showMessageDialog(null,"Category Name is already existed! Please enter new one");
}
else{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con1=DriverManager.getConnection("jdbc:mysql://localhost/possystem","root","");
pst = con1.prepareStatement("INSERT INTO category (category,status) VALUES (?,?)");
pst.setString(1, category);
pst.setString(2,status);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Category Added" );
table_update();
txtcat.setText("");
txtstatus.setSelectedIndex(1);
txtcat.requestFocus();
} catch (ClassNotFoundException ex) {
Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);
}
}
MySQL Table Name - category
MySQL Table Field Names - id, category, status
Text fields on JFrame - txtcat , status(combobox) )
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Add a category , add button
String category = txtcat.getText();
String status=txtstatus.getSelectedItem().toString();
//Duplicate check - not work
String dup = "SELECT category FROM category where category = '"+category+"'";
if(dup.equals(category)){
JOptionPane.showMessageDialog(null,"Category Name is already existed! Please enter new one");
}
else{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con1=DriverManager.getConnection("jdbc:mysql://localhost/possystem","root","");
pst = con1.prepareStatement("INSERT INTO category (category,status) VALUES (?,?)");
pst.setString(1, category);
pst.setString(2,status);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Category Added" );
table_update();
txtcat.setText("");
txtstatus.setSelectedIndex(1);
txtcat.requestFocus();
} catch (ClassNotFoundException ex) {
Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);
}
}