title: Insert declared variables into db Table using Java
body: <p>So I am having a slight problem inserting values into a table I created in netbeans. I will show you the code that works and creates a new worker in the table and then show you what goes wrong when I try to change it.</p>
  
  <p>This is a method from a class called dbConnect.java </p>
  
  <pre><code>public void insertTableRow() {
          try {
              Connection con = DriverManager.getConnection(host, uName, uPass);
              Statement stmt = con.createStatement();
              String SQL = "INSERT INTO Workers VALUES (10, 'John', 'Smith', 'Engineer')";
              stmt.executeUpdate(SQL);
          } catch (SQLException err) {
              System.out.println(err.getMessage());
          }
      }
  </code></pre>
  
  <p>And is here where I call it in the main class. </p>
  
  <pre><code>dbConnect test = new dbConnect();
  test.insertTableRow();
  </code></pre>
  
  <p>And then I get a John Smith appears so I know I have the right code. BUT when I try to enter in variables into VALUES it all falls apart. i.e. </p>
  
  <pre><code>public void insertTableRow(int id, String firstName, String lastName, String jobTitle) {
      try {
          int num = id;
          String fName = firstName;
          String lName = lastName;
          String jTitle = jobTitle;
          Connection con = DriverManager.getConnection(host, uName, uPass);
          Statement stmt = con.createStatement();
          String SQL = "INSERT INTO Workers VALUES (num, fName, lName, jTitle)";
          stmt.executeUpdate(SQL);
      } catch (SQLException err) {
          System.out.println(err.getMessage());
      }
  }
  </code></pre>
  
  <p>Combined with - </p>
  
  <pre><code>dbConnect test = new dbConnect();
  test.insertTableRow(10, "John", "Smith", "Doctor");
  System.out.println(test.getTableContents());
  </code></pre>
  
  <p>The error I get back is:- Column 'NUM' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then 'NUM' is not a column in the target table.</p>
  
  <p>So what am doing wrong because I have absolutely no idea?</p>