Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try{
- int lastinsertid=0;
- Class.forName("com.mysql.jdbc.Driver");
- java.sql.Connection con1=DriverManager.getConnection("jdbc:mysql://localhost/javasales","root","");
- String query = " insert into sales_product (product, price)"
- + " values (?, ?)";
- // create the mysql insert preparedstatement
- PreparedStatement preparedStmt = (PreparedStatement) con1.prepareStatement(query);
- preparedStmt.setString (1, txtproduct.getText());
- preparedStmt.setString (2, txtprice.getText());
- preparedStmt.executeUpdate();
- ResultSet rs = preparedStmt.executeQuery();
- if (rs.next()) {
- lastinsertid = (int) rs.getLong(1);
- }
- System.out.println("Inserted record's ID: " + lastinsertid);
- }
- catch(ClassNotFoundException coe)
- {
- System.out.println("odbc driver not found");
- }
- catch(SQLException sqe)
- {
- System.out.println(sqe);
- }
Add Comment
Please, Sign In to add comment