在SQL语句中实现动态变量时,是否有比字符串连接更好的方法
技术问答
223 人阅读
|
0 人回复
|
2023-09-12
|
我正在使用Java工作方法如下:
( {- ^. d/ g( e1 t' D8 d! O+ Z" d 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;}正如你所看到的,我目前正在使用一个巨大的字符串从数据库中提取数据,并被告知这不是最好的解决方案。但遗憾的是,我没有被告知该怎么办。但我认为这是一种组合SQL句子是冒险的,我想知道其他获得相同结果的方法。- E: x, `/ C4 E$ h
9 n$ {2 {7 V( {6 w' q
解决方案: |
|
|
|
|
|