在 SQL 语句中实现动态变量时,有没有比字符串连接更好的方法
技术问答
206 人阅读
|
0 人回复
|
2023-09-12
|
我在 Java 工作,我有以下方法:# r& v6 a+ G- s! t
public ResultSet getRecordsWithinBoundingBox(int spillFarLeftValue,int spillFarRightValue,int spillMostDownwardValue,int spillMostUpwardValue) ResultSet resultSet = null; try Statement statement = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql = "SELECT * FROM OTH WHERE (jl=" spillFarLeftValue " AND ih=" spillFarLeftValue ")) OR (jh>=" spillMostDownwardValue " AND (ih>=" spillFarLeftValue " AND ih=" spillFarLeftValue ")) OR (il=" spillFarRightValue " AND (jl=" spillMostDownwardValue ") OR (jh>=" spillMostDownwardValue " AND jh>=" spillMostUpwardValue ")) OR (jl=" spillMostUpwardValue " AND (il>=" spillFarLeftValue " AND il=" spillFarLeftValue ")) OR (il=" spillFarRightValue " AND jl=" spillMostUpwardValue ")"; resultSet = statement.executeQuery(sql); statement.close( ); resultSet.close( )catch (SQLException ex) Logger.getLogger(DatabaseInteractor.class.getName()).log(Level.SEVERE,null,ex); } return resultSet;}
7 ]1 E4 m3 T _: I" _5 B5 M, w 正如你所看到的,我目前正在使用一个巨大的字符串从我的数据库中提取数据,但有人告诉我,这不是最好的解决方案。但不幸的是,我没有被告知该做什么。但我认为这是我现在的组合SQL 句子是有风险的,我想知道同样结果的替代方法。. |5 Q, F$ G4 X, t. T5 A# B
! Q# t5 m D$ [' w4 K" t 解决方案: |
|
|
|
|
|