在许多编程语言中,这样的语句可以用于准备语句:
PreparedStatement statement = connection.prepareStatement(
"SELECT id FROM Company WHERE name LIKE ${name}");
statement.setString("name", "IBM");
Run Code Online (Sandbox Code Playgroud)
但不是java.sql.PreparedStatement.在Java中,必须使用参数索引:
PreparedStatement statement = connection.prepareStatement(
"SELECT id FROM Company WHERE name LIKE ?");
statement.setString(1, "IBM");
Run Code Online (Sandbox Code Playgroud)
有没有像第一个例子中那样使用字符串变量的解决方案?"$ {.*}"是否未在SQL语言中的其他位置使用,或者是否存在冲突?因为我会自己实现它(解析SQL字符串并用"?"替换每个变量,然后用Java方式).
问候,凯
我将参数传递给PreparedStatement,如下所示:
public void getNodes(String runId, File file, Connection conn) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(Mat.queries.get("NettingNode.QUERY"));
ps.setString(1, runId);
ps.setFetchSize(10000);
rs = ps.executeQuery();
// etc.
} catch (Exception e) {
logger.error(e, e);
} finally {
close(rs, ps);
}
}
Run Code Online (Sandbox Code Playgroud)
查询看起来像这样:
select * from table_1 where run_id = ?
Run Code Online (Sandbox Code Playgroud)
现在我想像这样修改我的查询,并重用第一个参数(两者?
都使用runId参数):
select * from table_1 where run_id = ?
union
select * from table_2 where run_id = ?
Run Code Online (Sandbox Code Playgroud)
没有这样做可能是这样的:
ps.setString(1, runId);
ps.setString(2, runId);
Run Code Online (Sandbox Code Playgroud) 我将 JOOQ 与普通/原始 SQL 一起使用,这意味着我没有使用任何代码生成或流畅的 DSL 东西。
以下代码有效:
Connection connection = ...;
DSLContext context = DSL.using(connection, ...);
String sql = "select * from mytable t where (t.id = ?)";
String id = ...; //
Result<Record> result = context.fetch(sql, id);
Run Code Online (Sandbox Code Playgroud)
现在假设我有一个带有多个参数的查询,如下所示:
String sql = "select * from mytable t where (t.id = ?) " +
"and (t.is_active = ?) and (t.total > ?)";
Run Code Online (Sandbox Code Playgroud)
如何在这些类型的查询中使用命名参数?我在想这样的事情:
String sql = "select * from mytable t where (t.id = :id) " +
"and (t.is_active = …
Run Code Online (Sandbox Code Playgroud) 我必须编写一个查询,其中条件参数未知,因为它们是在jdbc中动态设置的.这些条件应该是可选的.我用的是h2数据库.查询是:
select e.event_id,a.attempt_id,a.preferred,a.duration,a.location
from event e,attempt a
where e.user_label=? and e.start_time=?
and e.end_time=? and e.duration_min=?
and e.duration_max=?
and e.event_id=a.event_id
Run Code Online (Sandbox Code Playgroud)
但是如何使用OR来使这些条件可选,因为参数不知道?
谢谢!
我有这样的SQL查询.
select "DEPT"."DEPTNO" as "DEPTNO1",
"DEPT"."DNAME" as "DNAME1",
"DEPT"."LOC" as "LOC1",
"EMP"."COMM" as "COMM1",
"EMP"."EMPNO" as "EMPNO1",
"EMP"."ENAME" as "ENAME1",
"EMP"."HIREDATE" as "HIREDATE1",
"EMP"."JOB" as "JOB1",
"EMP"."MGR" as "MGR1",
"EMP"."SAL" as "SAL1"
from "EMP" , "DEPT" where "DEPT"."DEPTNO" in (:DeptNo)
Run Code Online (Sandbox Code Playgroud)
//这是Jdbc代码
Class.forName(DB_DRIVER);
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
Statement statment = dbConnection.createStatement();
result = statment.execute(query);//query is above sql query
Run Code Online (Sandbox Code Playgroud)
当我在Oracle上运行以上查询时,sql开发人员工作得很完美.但是当我用上面的jdbc代码运行它时,它会抛出并非所有变量绑定异常.如何在JDBC中运行上面的查询