Nashorn ?? JDBC ? Oracle DB ?????·?? 3
- by Homma
????
Nashorn ?? JavaScript ??????? JDBC ? Oracle DB ???????????????????? Oracle DB ????? SQL ???????????????
????????????????????????????????
????????? URL ? https://blogs.oracle.com/nashorn_ja/entry/nashorn_jdbc_3 ???
JDBC ???????????????
JDBC ????????????????? Nashorn ????? JavaScript ?????????????
???????? JDBC OCI ????????????????????????????????
?????
?? Java ??????????????? Nashorn ? JavaScript ????????????????
// Invoke jjs with -scripting option.
/*
* This sample can be used to check the JDBC installation.
* Just run it and provide the connect information. It will select
* "Hello World" from the database.
*/
var OracleDataSource = Java.type("oracle.jdbc.pool.OracleDataSource");
function main() {
// Prompt the user for connect information
print("Please enter information to test connection to the database");
var user, password, database;
user = readLine("user: ");
slash_index = user.indexOf('/');
if (slash_index != -1)
{
password = user.substring(slash_index + 1)
user = user.substring(0, slash_index);
}
else
password = readLine("password: ");
database = readLine("database(a TNSNAME entry): ");
java.lang.System.out.print("Connecting to the database...");
java.lang.System.out.flush();
print("Connecting...");
// Open an OracleDataSource and get a connection
var ods = new OracleDataSource();
ods.setURL("jdbc:oracle:oci:@" + database);
ods.setUser(user);
ods.setPassword(password);
var conn = ods.getConnection();
print("connected.");
// Create a statement
var stmt = conn.createStatement();
// Do the SQL "Hello World" thing
var rset = stmt.executeQuery("select 'Hello World' from dual");
while (rset.next())
print(rset.getString(1));
// close the result set, the statement and the connection
rset.close();
stmt.close();
conn.close();
print("Your JDBC installation is correct.");
}
main();
oracle.jdbc.pool.OracleDataSource ? Java.type() ?????Nashorn ???????????????????????????
Java ? System.out.println() ? System.out.flush() ? java.lang. ????????????????
Java ?????????? readEntry() ????? Nashorn ? readLine() ????????????
Java ????????????????????????JavaScript ??????????????????
?? Java ??????????????????????????? Java ???????????????? JavaScript ??????????????????
????????
JDBC OCI ???????????????? LD_LIBRARY_PATH ?????????????????
???Nashorn ? readLine() ??????????jjs ????? -scripting ?????????????????
$ export LD_LIBRARY_PATH=${ORACLE_HOME}/lib
$ jjs -scripting -cp ${ORACLE_HOME}/jdbc/lib/ojdbc6.jar JdbcCheckup.js
Please enter information to test connection to the database
user: test
password: test
database(a TNSNAME entry): orcl
Connecting to the database...Connecting...
connected.
Hello World
Your JDBC installation is correct.
JDBC OCI ????????????????? "select 'Hello World' from dual" ??? SQL ??????????????
?????????????????database ???? :: ???????????
???
??? Oracle DB ????? SQL ????????????????
Java ? JDBC ??????????????????????????? Nashorn ??????????????????????????????????