Guest User

Untitled

a guest
Jan 9th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. try{
  2. int lastinsertid=0;
  3. Class.forName("com.mysql.jdbc.Driver");
  4. java.sql.Connection con1=DriverManager.getConnection("jdbc:mysql://localhost/javasales","root","");
  5. String query = " insert into sales_product (product, price)"
  6. + " values (?, ?)";
  7.  
  8. // create the mysql insert preparedstatement
  9. PreparedStatement preparedStmt = (PreparedStatement) con1.prepareStatement(query);
  10. preparedStmt.setString (1, txtproduct.getText());
  11. preparedStmt.setString (2, txtprice.getText());
  12.  
  13. preparedStmt.executeUpdate();
  14. ResultSet rs = preparedStmt.executeQuery();
  15. if (rs.next()) {
  16. lastinsertid = (int) rs.getLong(1);
  17. }
  18. System.out.println("Inserted record's ID: " + lastinsertid);
  19. }
  20. catch(ClassNotFoundException coe)
  21. {
  22. System.out.println("odbc driver not found");
  23. }
  24. catch(SQLException sqe)
  25. {
  26. System.out.println(sqe);
  27. }
Add Comment
Please, Sign In to add comment