Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. private java.sql.Connection con = null;
  2. private PreparedStatement pst = null;
  3. private ResultSet rs = null;
  4. private String url = "jdbc:mysql://localhost:8889/deliveryEarn";
  5. private String user = "root";
  6. private String password = "root";
  7.  
  8. try {
  9. con = DriverManager.getConnection(url, user, password);
  10. Statement st = (Statement) con.createStatement();
  11.  
  12. st.executeUpdate("INSERT INTO incomeCalc " + "VALUES (3, 75, 6, 25, 18.50)");
  13.  
  14. con.close();
  15. }
  16.  
  17. catch (SQLException ex) {
  18. Logger lgr = Logger.getLogger(deliveryMain.class.getName());
  19. lgr.log(Level.SEVERE, ex.getMessage(), ex);
  20.  
  21. }
  22.  
  23. INSERT INTO incomeCalc VALUES (3, 75, 6, 25, 18.50) // error
  24. // the only way this will work is when you have only 5 columns in
  25. // your table but in your case you have 7 that is why it wll not work
  26.  
  27. INSERT INTO incomeCalc(specify columns here to the values bound to)
  28. VALUES (3, 75, 6, 25, 18.50)
  29.  
  30. INSERT INTO table_name
  31. VALUES (value1, value2, value3,...)
  32.  
  33. INSERT INTO table_name (column1, column2, column3,...)
  34. VALUES (value1, value2, value3,...)
  35.  
  36. "INSERT INTO incomeCalc(ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) " +
  37. "VALUES (3, 75, 6, 25, 18.50)"
  38.  
  39. "INSERT INTO incomeCalc (ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) VALUES (3, 75, 6, 25, 18.50)"
  40.  
  41. ps = "INSERT INTO incomeCalc(ID, TIPS, HOURS, GAS, HOURLY_EARNINGS) VALUES (?, ?, ?, ?, ?)"
  42.  
  43. ps.setInt(index++, id); //id=3
  44. ps.setInt(index++, tips); //tips=75
  45. ps.setInt(index++, hours);//hour=6
  46. ps.setInt(index++, gas);//gas=25
  47. ps.setFloat(index++, HOURLY_EARNINGS);
  48.  
  49. INSERT INTO TABLE_NAME (COLUMN_NAMES_SEPARATED_BY_COMMA) VALUES (VALUES_SEPARATED_BY_COMMA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement