Skip to content

informat.datasource Data Source Related Operations

Overview

Use informat.datasource to perform operations related to data sources

getConnection

Get a database connection from the connection pool

javascript
informat.datasource.getConnection(moduleId, dataSourceKey, setting);

TIP

  • Connection creation fails will throw an exception
  • When autoCommit is set to true, transactions will be automatically committed when executing update, insert, and delete statements
  • After the script execution is complete, you need to manually close the created connection
ParameterTypeDescription
moduleIdStringData source module identifier
dataSourceKeyStringData source identifier
settingDataSourceConnnectionSettingData source connection setting

Return Value Database connection, type JDBCConnection

Example

javascript
const connection = informat.datasource.getConnection("dataSource", "db_informat", {
  autoCommit: true,
});
connection.select("select * form table", (row) => {
  console.log(row.getString(1));
});

// Close the connection
connection.close();

Connection

select

Execute query, if query fails will throw exception

javascript
connection.select(sql, handler, ...args);
ParameterTypeDescription
sqlStringQuery SQL
handler(ResultSet)FunctionQuery result callback function
args...ObjectQuery parameters

Example 1

javascript
//查询 user 表中name等于tom的数据
connection.select(
  "select * from user where name=?",
  (rs) => {
    console.log(rs.getString("name"));
  },
  "tom"
);

Example 2 Using like

javascript
//查询 user 表中name包含tom的数据
connection.select(
  ("select * from user where name like concat(" % ",?,") % ")",
  (rs) => {
    console.log(rs.getString("name"));
  },
  "tom"
);

Example 3 Using in

javascript
//查询 user 表中name是tom或jerry的数据
connection.select(
  "select name from user where name in(?,?)",
  (rs) => {
    console.log(rs.getString("name"));
  },
  "tom",
  "jerry"
);

insert

Execute insert, if insert fails will throw exception

javascript
connection.insert(sql, returnAutoGeneratedKeys, ...args);
ParameterTypeDescription
sqlStringQuery SQL
returnAutoGeneratedKeysBooleanWhether to return auto-generated primary key ID
args...ObjectInsert parameters

Return Value

Type String

If returnAutoGeneratedKeys is set to true and the operation is insert, it will return the auto-generated primary key ID of the last inserted data

Example

javascript
connection.insert("insert into user(name,age) values (?, ?)", true, "tom", 10);

update

Execute update or delete, if update or delete fails will throw exception

javascript
connection.update(sql, ...args);
ParameterTypeDescription
sqlStringQuery SQL
args...ObjectUpdate parameters

Return Value Type Integer

Returns the number of records successfully updated or deleted

Example

javascript
connection.update("update user set age=?", 10);
javascript
connection.update("delete from user where age=?", 10);

commit

Commit transaction, if commit transaction fails will throw exception

javascript
connection.commit();

rollback

Roll back transaction, if roll back transaction fails will throw exception

javascript
connection.rollback();

close

Close the database connection

javascript
connection.close();

ResultSet

TIP

columnIndex 为索引时,起始索引为 1

MethodReturn ValueDescription
getString(columnIndex)StringGet String type column by index or name
getBoolean(columnIndex)BooleanGet Boolean type column by index or name
getByte(columnIndex)ByteGet Byte type column by index or name
getShort(columnIndex)ShortGet Short type column by index or name
getInt(columnIndex)IntegerGet Integer type column by index or name
getLong(columnIndex)LongGet Long type column by index or name
getFloat(columnIndex)FloatGet Float type column by index or name
getDouble(columnIndex)DoubleGet Double type column by index or name
getBytes(columnIndex)Byte[]Get byte[] type column by index or name
getDate(columnIndex)DateGet Date type column by index or name
getTime(columnIndex)TimeGet Time type column by index or name
getTimestamp(columnIndex)TimestampGet Timestamp type column by index or name
getObject(columnIndex)ObjectGet Object type column by index or name
getBigDecimal(columnIndex)BigDecimalGet BigDecimal type column by index or name
getColumnCount()IntegerReturns the number of columns in the result set
getColumnName(columnIndex)StringReturns the name of the column at the specified index
getColumnTypeName(columnIndex)StringReturns the type name of the column at the specified index