Orange Script 通过JDBC连接数据库的例子

发布时间:2014-10-23 23:23:17
来源:分享查询网

详细代码,请看这里 var conn = $.java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test", "root", ""); var dropTable = function() {     var stmt = conn.createStatement();     stmt.execute("DROP TABLE IF EXISTS foo");     stmt.close(); }; var createTable = function() {     var stmt = conn.createStatement();     stmt.execute("CREATE TABLE foo (city VARCHAR(30), state VARCHAR(2), population INT)");     stmt.close(); }; var filldata = function(city, state, population) {     var stmt = conn.prepareStatement("INSERT INTO foo(city, state, population) VALUES (?,?,?)");     stmt.setString(1, city);     stmt.setString(2, state);     stmt.setInt(3, population);     stmt.executeUpdate();     stmt.close(); }; var query = function() {     var stmt = conn.prepareStatement("SELECT * FROM foo WHERE state = ?");     stmt.setString(1, "WA");     var rs = stmt.executeQueryPrepared();     while (rs.next()) {         println(rs.getString("city"));     }     rs.close();     stmt.close(); }; // delete cities in CA var deleteCA = function() {     var stmt = conn.prepareStatement("DELETE FROM foo WHERE state = ?");     stmt.setString(1, "CA");     stmt.executeUpdate();     stmt.close(); }; var doublePopulation = function() {     var stmt = conn.prepareStatement("UPDATE foo SET population = population * 2");     stmt.executeUpdate();     stmt.close(); }; dropTable(); createTable(); filldata("Bellevue", "WA", 122); filldata("Sammamish", "WA", 45); filldata("San Francisco", "CA", 825); println("Cities in WA"); println("================="); query(); println("Double population for all cities"); doublePopulation(); println("Cities in CA has been deleted"); deleteCA(); conn.close();

返回顶部
查看电脑版