· 8 years ago · Feb 19, 2018, 11:06 AM
1import java.sql.*;
2
3public class Main {
4 private static String url = "jdbc:sqlite::memory";
5
6 private static void createNewTable() {
7
8 String sql = "CREATE TABLE IF NOT EXISTS students (n"
9 + " id integer PRIMARY KEY,n"
10 + " name text NOT NULL"
11 + ");";
12
13 try (Connection conn = DriverManager.getConnection(url);
14 Statement stmt = conn.createStatement()) {
15 stmt.execute(sql);
16 } catch (SQLException e) {
17 System.out.println(e.getMessage());
18 }
19 }
20
21 private static void checkTable(){
22 String sql = "SELECT name FROM sqlite_temp_master WHERE type='table'";
23
24 try (Connection conn = DriverManager.getConnection(url);
25 Statement stmt = conn.createStatement()) {
26 ResultSet rs = stmt.executeQuery(sql);
27
28 while (rs.next()) {
29 System.out.println("table: " + rs.getString(1));
30 }
31 } catch (SQLException e) {
32 System.out.println(e.getMessage());
33 }
34 }
35
36 public static void main(String[] args) {
37 createNewTable();
38 checkTable();
39 }
40}