Programming help

B.Chat

Well-known member
  • Jun 28, 2021
    10,243
    20,779
    113
    (මේ ජවා ටිකේ රතුපාටින් තියෙන ටික මම 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);
    }

    }
     

    Lupine Solus

    Well-known member
  • Dec 2, 2022
    734
    1,669
    93
    Code එක මෙහෙම දාන්න මචන් ආයෙ. (More options => code)

    Java:
    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);
                }
    
            }
     
    • Love
    Reactions: B.Chat

    B.Chat

    Well-known member
  • Jun 28, 2021
    10,243
    20,779
    113
    Code එක මෙහෙම දාන්න මචන් ආයෙ. (More options => code)

    Java:
    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);
                }
    
            }
    Thanks bro 👍 මම එහෙමත් දැම්මා හරිගියේ නෑ. හැබැයි error එකක් නෑ. Duplicate චෙක් වෙන්නේ නෑ කලින් code
    එකේ වගේම
     
    • Like
    Reactions: Lupine Solus

    kasun090354t

    Well-known member
  • Aug 21, 2011
    24,041
    36,036
    113
    කෑගල්ල
    Thanks bro 👍 මම එහෙමත් දැම්මා හරිගියේ නෑ. හැබැයි error එකක් නෑ. Duplicate චෙක් වෙන්නේ නෑ කලින් code
    එකේ වගේම
    මේ ඔය try කියන එකට යටින් අරන් තියන connection එක වගේ එකක් උඩත් ගන්න ඕනේ නේද dup එක ගන්නත්. ?
     
    • Wow
    Reactions: B.Chat

    B.Chat

    Well-known member
  • Jun 28, 2021
    10,243
    20,779
    113
    If එකට උඩින් msgbox එකක් දාලා බලපන් dup කියන එක හරියට වැඩද කියලා.
    dup එක හරියට output එක දෙනවා bro message එකේ
     

    0x9

    Well-known member
  • Mar 18, 2015
    541
    836
    93
    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");
    }
    SQL statement string එකක් value එකක් එක්ක compare කරලා වැඩක් නෑ නේ.. එක execute කරල results එකක් අරගෙන එක compare කරනම් හරිනේ

    Java:
    PreparedStatement ps = con1.prepareStatement("SELECT category FROM category where category = ?");
    ps.setString (1, category);
    ResultSet rs = ps.executeQuery();
    
    if (rs.next()) {
        JOptionPane.showMessageDialog(null,"Category Name is already existed! Please enter new one");
    } else {
        // insert flow
    }
     

    B.Chat

    Well-known member
  • Jun 28, 2021
    10,243
    20,779
    113
    මේ ඔය try කියන එකට යටින් අරන් තියන connection එක වගේ එකක් උඩත් ගන්න ඕනේ නේද dup එක ගන්නත්. ?
    එක නම් sure නෑ. හැබැයි කිව්වා වගේ if එකට උඩින් dup එකේ message එක හරියට එනවා value එක එක්ක
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    Java නම් දන්නේ නෑ හැබැයි ඔයා ඔතන කරලා තියෙන්නේ selecq query text එකයි txtcat.getText() වල අගයයි මැච් කරන එක. ඔතන වෙන්න ඕනෑ ඒක නෙමෙයි ඔය select query එක execute කරලා ගන්න අගය txtcat.getText() එක්ක මැච් කරන එකයි.

    Edit: @0x9 උඩ උත්තරේ දීලා තියෙනවා පොස්ට් එක දැම්මට පස්සෙයි දැක්කේ
     
    • Love
    Reactions: B.Chat

    B.Chat

    Well-known member
  • Jun 28, 2021
    10,243
    20,779
    113
    SQL statement string එකක් value එකක් එක්ක compare කරලා වැඩක් නෑ නේ.. එක execute කරල results එකක් අරගෙන එක compare කරනම් හරිනේ

    Java:
    PreparedStatement ps = con1.prepareStatement("SELECT category FROM category where category = ?");
    ps.setString (1, category);
    ResultSet rs = ps.executeQuery();
    
    if (rs.next()) {
        JOptionPane.showMessageDialog(null,"Category Name is already existed! Please enter new one");
    } else {
        // insert flow
    }
    Thanks bro 👍 වැඩේ ගොඩ 😀

    Java නම් දන්නේ නෑ හැබැයි ඔයා ඔතන කරලා තියෙන්නේ selecq query text එකයි txtcat.getText() වල අගයයි මැච් කරන එක. ඔතන වෙන්න ඕනෑ ඒක නෙමෙයි ඔය select query එක execute කරලා ගන්න අගය txtcat.getText() එක්ක මැච් කරන එකයි.

    Edit: @0x9 උඩ උත්තරේ දීලා තියෙනවා පොස්ට් එක දැම්මට පස්සෙයි දැක්කේ
    ඔව් එක තමයි අවුල දැන් හරි 👍😀
    ------ Post added on Jan 8, 2023 at 6:13 PM

    Help කරපු හැමෝටම thanks 👍😍
    ------ Post added on Jan 8, 2023 at 6:15 PM