@@ -125,6 +125,35 @@ function writeManyRecords() {
125125 console . log ( 'Failed with an error %s' , err . message ) ;
126126 }
127127}
128+
129+ /**
130+ * Write 500 rows of data to a table in a single batch.
131+ * Recommended for faster writes
132+ */
133+ function writeManyRecordsUsingExecuteBatch ( ) {
134+ try {
135+ const conn = Jdbc . getCloudSqlConnection ( dbUrl , user , userPwd ) ;
136+ conn . setAutoCommit ( false ) ;
137+
138+ const start = new Date ( ) ;
139+ const stmt = conn . prepareStatement ( 'INSERT INTO entries ' +
140+ '(guestName, content) values (?, ?)' ) ;
141+ const params = [ ] ;
142+ for ( let i = 0 ; i < 500 ; i ++ ) {
143+ params . push ( [ 'Name ' + i , 'Hello, world ' + i ] ) ;
144+ }
145+
146+ const batch = stmt . executeBatch ( params ) ;
147+ conn . commit ( ) ;
148+ conn . close ( ) ;
149+
150+ const end = new Date ( ) ;
151+ console . log ( 'Time elapsed: %sms for %s rows.' , end - start , batch . length ) ;
152+ } catch ( err ) {
153+ // TODO(developer) - Handle exception from the API
154+ console . log ( 'Failed with an error %s' , err . message ) ;
155+ }
156+ }
128157// [END apps_script_jdbc_write]
129158
130159// [START apps_script_jdbc_read]
@@ -158,4 +187,36 @@ function readFromTable() {
158187 console . log ( 'Failed with an error %s' , err . message ) ;
159188 }
160189}
190+
191+ /**
192+ * Read up to 1000 rows of data from the table and log them.
193+ * Recommended for faster reads
194+ */
195+ function readFromTableUsingGetRows ( ) {
196+ try {
197+ const conn = Jdbc . getCloudSqlConnection ( dbUrl , user , userPwd ) ;
198+ const start = new Date ( ) ;
199+ const stmt = conn . createStatement ( ) ;
200+ stmt . setMaxRows ( 1000 ) ;
201+ const results = stmt . executeQuery ( 'SELECT * FROM entries' ) ;
202+ const numCols = results . getMetaData ( ) . getColumnCount ( ) ;
203+ const getRowArgs = [ ] ;
204+ for ( let col = 0 ; col < numCols ; col ++ ) {
205+ getRowArgs . push ( `getString(${ col + 1 } )` ) ;
206+ }
207+ const rows = results . getRows ( getRowArgs . join ( ',' ) ) ;
208+ for ( let i = 0 ; i < rows . length ; i ++ ) {
209+ console . log ( rows [ i ] . join ( '\t' ) ) ;
210+ }
211+
212+ results . close ( ) ;
213+ stmt . close ( ) ;
214+
215+ const end = new Date ( ) ;
216+ console . log ( 'Time elapsed: %sms' , end - start ) ;
217+ } catch ( err ) {
218+ // TODO(developer) - Handle exception from the API
219+ console . log ( 'Failed with an error %s' , err . message ) ;
220+ }
221+ }
161222// [END apps_script_jdbc_read]
0 commit comments