cockroachdb / docs

CockroachDB user documentation
https://cockroachlabs.com/docs
Creative Commons Attribution 4.0 International
187 stars 453 forks source link

Use JDBI to execute basic SQL statements in JDBC samples #10014

Open ericharmeling opened 3 years ago

ericharmeling commented 3 years ago

Eric Harmeling (ericharmeling) commented:

@rafiss

Jira Issue: DOC-1032

rafiss commented 3 years ago

See https://github.com/cockroachdb/docs/pull/9998 for where this thought came from:

with vanilla JDBC you need to do

PreparedStatement p = connection.prepareStatement("DELETE from promo_codes WHERE code IN(?, ?, ?)");
p.setString(1, codeOne);
p.setString(2, codeTwo);
p.setString(3, codeThree);
p.executeUpdate(); 

The placeholder arguments are important. Using normal string concatenation leaves the code open to SQL injection, so we don't want to have string concatenation in our docs. But placeholders in JDBC are cumbersome. JDBI (https://jdbi.org/) lets you do something simpler:

handle.execute("DELETE from promo_codes WHERE code IN(?, ?, ?)", codeOne, codeTwo, codeThree);
github-actions[bot] commented 1 year ago

We have marked this issue as stale because it has been inactive for 18 months. If this issue is still relevant, removing the stale label or adding a comment will keep it active. Otherwise, we'll close it in 10 days to keep the issue queue tidy. Thank you for your contribution to CockroachDB docs!