sfs 2x執行SQL語法
在sfs 2x執行SQL語法
是透過IDBManager物件來處理
如果要傳入參數給SQL語法的話
要用PreparedStatement物件來處理
取出陣列,直接傳給Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
IDBManager dbManager = getParentExtension().getParentZone().getDBManager(); String sql = "SELECT * FROM people" ; try { ISFSArray res = dbManager.executeQuery(sql); ISFSObject response = new SFSObject(); response.putSFSArray( "people" , res); send( "getPeople" , response, sender); } catch (SQLException e) { trace(ExtensionLogLevel.WARN, "SQL Failed: " + e.toString()); } |
將參數傳入SQL語法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
IDBManager dbManager = getParentExtension().getParentZone().getDBManager(); Connection connection; try { connection = dbManager.getConnection(); PreparedStatement stmt; ResultSet res; stmt = connection.prepareStatement( "SELECT * FROM user WHERE user_id = ?" ); stmt.setString( 1 , "5" ); res = stmt.executeQuery(); trace(res.getInt( "name" )); } catch (SQLException e) { trace(ExtensionLogLevel.WARN, "SQL Failed: " + e.toString()); } |