Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

43
LINES

< > BotCompany Repo | #1011905 // Connect to H2 server, insert stuff (OK)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 1780K of libraries. Click here for Pure Java version (890L/6K).

!7

lib 1011900 // h2

static Connection conn;

import java.sql.*;
import java.lang.reflect.Array;

p {
  Class.forName("org.h2.Driver");
  conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test", "sa", "");
  
  print(symbolToID(print("hello"));
  print(symbolToID(print("world"));
  
  conn.close();
}

static int symbolToID(S symbol) ctex {
  PreparedStatement statement = conn.prepareStatement("select id from symbol where name=?");
  statement.setString(1, symbol);
  ResultSet rs = statement.executeQuery();
  try {
    if (rs.next())
      ret rs.getInt("id");
  } finally {
    rs.close();
  }
  
  statement = conn.prepareStatement("insert into symbol set name=?",
    statement.RETURN_GENERATED_KEYS);
  statement.setString(1, symbol);
  statement.executeUpdate();

  ResultSet generatedKeys = statement.getGeneratedKeys();
  try {
    assertTrue(generatedKeys.next());
    ret generatedKeys.getInt(1);
  } finally {
    generatedKeys.close();
  }
}

Author comment

Began life as a copy of #1011903

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011905
Snippet name: Connect to H2 server, insert stuff (OK)
Eternal ID of this version: #1011905/14
Text MD5: 7e5d7a3ca42bb3ae0466e0d6b0f49f48
Transpilation MD5: f047c5d308166b5537bf0b9006786c53
Author: stefan
Category: javax / h2 database
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-09 10:58:08
Source code size: 1013 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 422 / 1023
Version history: 13 change(s)
Referenced in: [show references]