001: /*
002: * Copyright (C) 2003-2007 Kepler Project.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining
005: * a copy of this software and associated documentation files (the
006: * "Software"), to deal in the Software without restriction, including
007: * without limitation the rights to use, copy, modify, merge, publish,
008: * distribute, sublicense, and/or sell copies of the Software, and to
009: * permit persons to whom the Software is furnished to do so, subject to
010: * the following conditions:
011: *
012: * The above copyright notice and this permission notice shall be
013: * included in all copies or substantial portions of the Software.
014: *
015: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
018: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
019: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
020: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
021: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022: */
023:
024: package org.keplerproject.luajava.test;
025:
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028: import java.sql.Statement;
029:
030: import org.keplerproject.luajava.LuaState;
031: import org.keplerproject.luajava.LuaStateFactory;
032:
033: /**
034: * Uses JDBC statement to execute queries inside Lua.
035: * Uses hsqldb.
036: * @author thiago
037: */
038: public class TestJDBC {
039:
040: public static void main(String[] args) throws Exception {
041: // gets a java.sql.Connection and creates a Statement
042: Class.forName("org.hsqldb.jdbcDriver");
043: Connection con = DriverManager.getConnection(
044: "jdbc:hsqldb:hsql://localhost:9002", "sa", "");
045: Statement st = con.createStatement();
046:
047: try {
048: st.execute("DROP TABLE luatest");
049: } catch (Exception ignore) {
050: }
051:
052: st
053: .execute("CREATE TABLE luatest (id int not null primary key, str varchar, number double)");
054:
055: for (int i = 0; i < 10; i++) {
056: st
057: .executeUpdate("INSERT INTO luatest (id, str, number) values("
058: + i
059: + ", '"
060: + 2
061: * i
062: + "', "
063: + System.currentTimeMillis() + ")");
064: }
065:
066: LuaState L = LuaStateFactory.newLuaState();
067: L.openLibs();
068:
069: //L.pushString("st");
070: L.pushObjectValue(st);
071: //L.setTable(LuaState.LUA_GLOBALSINDEX.intValue());
072: L.setGlobal("st");
073:
074: int err = L.LdoFile("testJDBC.lua");
075: if (err != 0) {
076: switch (err) {
077: case 1:
078: System.out.println("Runtime error. " + L.toString(-1));
079: break;
080:
081: case 2:
082: System.out.println("File not found. " + L.toString(-1));
083: break;
084:
085: case 3:
086: System.out.println("Syntax error. " + L.toString(-1));
087: break;
088:
089: case 4:
090: System.out.println("Memory error. " + L.toString(-1));
091: break;
092:
093: default:
094: System.out.println("Error. " + L.toString(-1));
095: break;
096: }
097: }
098:
099: L.close();
100: st.close();
101: con.close();
102: }
103: }
|