Access the database using JS [non-Node. js]

Source: Internet
Author: User

When it comes to server-side JavaScript, many people first respond to Node. js. Java 6 contains the Script Engine, which comes with a JavaScript interpreter implemented by Mozilla Rhino-pure Java of the "castrated version.
Use jrunscript to start the interpreter. The advantage of using Rhino is that you can use the JavaScript language for development, but you can also use the vast available Java library! It also supports compiling into class files.
I use the connection to the Sqlite database as an example. First, create an sqlite database:
[SQL]
Sqlite λ sqlite3 user. db
SQLite version 3.7.3
Enter ". help" for instructions
Enter SQL statements terminated with ";"
Sqlite>. header on
Sqlite> select * from users;
Id | name | age
1 | Joe | 24
2 | redraiment | 24
3 | Kewell | 30
Sqlite>. quit
Sqlite λ
JS encapsulation JDBC
[JavaScript]
Var with_connection = function (url ){
Var connection = java. SQL. DriverManager. getConnection (url );
For (var argc = 1; argc <arguments. length; argc ++ ){
Arguments [argc]. call (connection );
}
Connection. close ();
};
 
Var with_query = function (SQL, fn ){
Return function (){
Var call = this. prepareStatement (SQL );
Var rs = call.exe cuteQuery ();
Var meta = rs. getMetaData ();
Var list = [];
While (rs. next ()){
Var o = {};
For (var I = 1; I <= meta. getColumnCount (); I ++ ){
O [meta. getColumnName (I)] = rs. getObject (I );
}
List. push (o );
}
Rs. close ();
Call. close ();
Return fn (list );
};
};
Use: read table information
[JavaScript]
New org. sqlite. JDBC ();
With_connection (
'Jdbc: sqlite: user. db ',
With_query ('select * from users', function (rs ){
For (var row = 0; row <rs. length; row ++ ){
For (var columnName in rs [row]) {
Printf ('% s => % s \ n', columnName, rs [row] [columnName]);
}
Print ('\ n ');
}
})
);
Execution result
Sqlite λ jrunscript-cp "$ CLASSPATH: $ PWD/sqlite-jdbc-3.7.2.jar" jdbc. js
Id => 1
Name => Joe
Age => 24

Id => 2
Name => redraiment
Age => 24

Id => 3
Name => Kewell
Age => 30

Sqlite λ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.