简体   繁体   中英

Java MySQL query named parameters?

Is there a way to have named parameters in Java MySQL query?

Like this:

SELECT * FROM table WHERE col1 = :val1 AND col2 = :val2

instead of this:

SELECT * FROM table WHERE col1 = ? AND col2 = ?

UPDATE: Im' using java.sql.* , however would be interested in alternatives capable of this.

Maybe Hibernate is good choice for you. It provided the query style as your description, and it's so powerful and convenient to do persistence work that you'll feel cool. eg

Query query = sesion.createQuery("from Student s where s.age = :age");
query.setProperties(student);


see the doc: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setParameter(java.lang.String, java.lang.Object)

出色的JDBI库使您可以做到这一点以及更多。

Yes, there are alternatives. By example, with javax.persistence.* you can achieve that quite easily.

http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html

An entity manager will allow you to create dynamic (parametrized) queries with the methods EntityManager.createQuery , and EntityManager.createNamedQuery .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM