PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users VALUES (?, ?, ?)"); pstmt.setString(1, "John"); pstmt.setString(2, "Doe"); pstmt.setShort(3, (short) 25); pstmt.executeUpdate();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products VALUES (?, ?, ?)"); pstmt.setString(1, "Product 1"); pstmt.setShort(2, (short) 100); pstmt.setShort(3, (short) 50); pstmt.addBatch(); pstmt.setString(1, "Product 2"); pstmt.setShort(2, (short) 200); pstmt.setShort(3, (short) 75); pstmt.addBatch(); pstmt.executeBatch();In both examples, the setShort() method is used to set a short value for the third parameter of the prepared statement. This method is used to make the statement parameterized, which helps to protect against SQL injection attacks. The method takes two parameters – the index of the parameter to set, and the short value to set it to.