1

Im aware that this question is rampant here in stackoverflow. But all that i've seen so far didnt help me at all. Here is my code:

My public class:

public class Strong extends javax.swing.JFrame {

  String a;

    public Apply() {
        initComponents();
         jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener()
        {
            @Override
            public void valueChanged(ListSelectionEvent e)
            {
                int selectedRow = jTable1.getSelectedRow();
                a = (String) jTable1.getValueAt(selectedRow, 0);


            }
        }
        );

    }

My Query:

try{

           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/application","root","");
            String sql= "insert into Mycart values(?,?,?)";
            PreparedStatement pst = con.prepareStatement(sql);            
            pst.setString(1, (String)a);
            pst.setString(2, (String)a);
            pst.setString(3, (String)a);

            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Added to Cart");

        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

i've read that this is an AUTO_INCREMENT problem. Which make sense since i've set two of my Table Columns to INT. Should i convert String to INT first? Or is there other solutions to this?

  • Bit off topic but do not let your class Strong extend from JFrame, it is consider as bad practice. You can better have a Jframe object within your Strong class, see: http://stackoverflow.com/questions/35911977/what-is-best-way-to-initializing-gui-fields/35912626#35912626 – M. Suurland Mar 14 '16 at 13:22
  • Is there a reason your column type is INT? Probably you only want integer numbers in it? Then you cannot insert a string, because a string is not a number. So yes, you should convert the string to an integer first. And be careful, you should catch `NumberFormatException`. – vanje Mar 14 '16 at 13:35
  • Noted, im still new to this btw. – Chester Vacalares Mar 14 '16 at 13:46
  • Yes, those column was for the Phone Numbers. Do you have any suggestions on what is the safest way to convert my String to Integer w/o compromising the code? I mean, im sure my code is not that complicated and sophisticated but i like it that way cause i can actually understand the logic of it. Its hard to defend codes i do not understand(Yes, this is actually a project for my Programming class). – Chester Vacalares Mar 14 '16 at 13:50
  • Nowadays Google is the best tool to find out such things. – vanje Mar 14 '16 at 14:47
  • Well, i thought this is stackoverflow. You know, where you can ask professionals about programming. I did looked it up on google and youtube. Im trying codes right now as we speak. What i need is a more personal advice since you did see my codes and know what is more relevant and irrelevant methods to use. – Chester Vacalares Mar 14 '16 at 23:38
  • Come on, converting a string to integer is such a common thing and a very basic operation, so the web is full of articles and tutorials about that. It is only a Google search away. You should show some initiative at least trying to solve your problems by yourself. Do some research (e.g. Google for it, read the API docs for some operations you found, ...) and then try something and look, if it works or not. Don't wait for others to present you the final solution. If you tried something and it doesn't work, you can show us the code and we are willing to help. – vanje Mar 15 '16 at 12:37
  • Sir, w/ all due respect, i did. I simply used all my resources around me INCLUDING Stackoverflow. I consulted the web, YT, i even asked my professor for help. So i dont really need someone to tell me that im not doing enough research regarding this. Yes, converting Strings to Integer is a basic thing. I know that at least! But the fact that im asking this in a place full of programmers tells you that im at the beginner level. I started learning the basic 6 months ago, and GUI 3 months ago. All i need is a simple advice, i dont need someone to refer me to google as if im not doing that already – Chester Vacalares Mar 15 '16 at 12:49
  • P.S. I solved it. It's just disappointing that all i got from my question is a little pep talk instead of an actual advice regarding my codes. Maybe try to be a little considerate w/ us beginners. Thanks anyways. – Chester Vacalares Mar 15 '16 at 12:51
  • Hey, it's great that you solved it, really. Even if you don't believe me, this way you learned far more. It is obvious that you are a beginner. There is no evil intent in holding back the direct solution. – vanje Mar 15 '16 at 14:14

0 Answers0