SELECT 'OskarDevelopers' FROM dual;
* DB에 있는 Table, Column, View 정보 보기.
-- Table
SELECT * FROM all_tables;
-- Column
SELECT * FROM all_tab_columns;
-- View
SELECT * FROM all_views;
SELECT 'OskarDevelopers' FROM dual;
-- Table
SELECT * FROM all_tables;
-- Column
SELECT * FROM all_tab_columns;
-- View
SELECT * FROM all_views;
SELECT 'OskarDevelopers' FROM sysibm.sysdummy1;
-- Table
SELECT * FROM syscat.tables;
-- Column
SELECT * FROM syscat.columns;
-- View
SELECT * FROM sysibm.sysviews;
package kr.oskarDevelopers.martin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;
public class TestDriver {
private static final String DB_DRIVER = "org.h2.Driver";
private static final String DB_CONNECTION = "jdbc:h2:~/testdb"; // database name
private static final String DB_USER = "martin"; // user id
private static final String DB_PASSWORD = "1234"; // passward
public static void main(String[] args) {
try {
DeleteDbFiles.execute("~", "testdb", true); // drop db if exist 'testdb'
initDB();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
private static void initDB() throws SQLException {
Connection connection = getDBConnection();
Statement stmt = null;
try {
connection.setAutoCommit(false);
stmt = connection.createStatement();
// create TEST_TABLE for example
stmt.execute("CREATE TABLE TEST_TABLE(idx INT PRIMARY KEY, name VARCHAR(100));");
// insert some values into TEST_TABLE
stmt.execute("INSERT INTO TEST_TABLE VALUES(1, 'Martin.Park'), (2, 'OskarDevelopers');");
// get result by using SELECT query
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_TABLE;");
while (rs.next()) {
System.out.println("idx : " + rs.getString("idx") + " / " + "name : " + rs.getString("name"));
}
stmt.close();
connection.commit();
} catch (SQLException e) {
System.out.println("Exception Message " + e.getLocalizedMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
}
}
package hanoiTower;
public class Hanoi {
private int x = 3; // fixed. A number of pillar.
private int y = -1; // height
private int[][] hanoiField;
private int[] cntLine = new int[x];
private int lastFoundLine = -1;
private int lastMovedLine = 1; // choose the place to move. (1 or 2)
private int moveCNT = -1; // moved times count
public Hanoi(int num) {
this.y = num;
this.hanoiField = new int[num][x];
}
public void initHanoiTower() {
System.out.println("Start!");
System.out.println("==========" + "\n");
// make HanoiTower
for (int i = y; i > 0; i--) {
hanoiField[i - 1][0] = i;
}
moveTower(y);
System.out.println("==========");
System.out.println("Done!");
System.out.println("Total moved count : " + moveCNT);
}
public void showTower() {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
String s = String.valueOf(hanoiField[i][j]);
if (s.length() == 1 || s.equals("0")) {
System.out.print("| " + s.replace("0", " ")
+ blank(s.length()));
} else {
System.out.print("| " + s + blank(s.length()));
}
}
System.out.print("|");
System.out.println();
}
System.out.println("----------------" + "\n");
moveCNT++;
}
public int countLine(int num) {
for (int i = 0; i < x; i++) {
int cnt = 0;
for (int j = 0; j < y; j++) {
if (hanoiField[j][i] != 0) {
cnt = cnt + 1;
}
}
cntLine[i] = cnt;
}
return cntLine[num];
}
public int[] findNumPlace(int num) {
int[] coordinate = new int[2];
coordinate[0] = -1;
coordinate[1] = -1;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (hanoiField[i][j] == num) {
lastFoundLine = j;
coordinate[0] = j;
coordinate[1] = i;
}
}
}
return coordinate;
}
public void moveTower(int num) {
int to = -1;
if (num <= 2) {
showTower();
// move 1, 2
for (int i = 1; i <= 2; i++) {
hanoiField[findNumPlace(i)[1]][findNumPlace(i)[0]] = 0;
to = x - (lastFoundLine + lastMovedLine);
hanoiField[(y - 1) - countLine(to)][to] = i;
lastMovedLine = to;
showTower();
}
// move 1 again
hanoiField[findNumPlace(1)[1]][findNumPlace(1)[0]] = 0;
hanoiField[(y - 1) - countLine(to)][to] = 1;
showTower();
} else {
// reflexive
moveTower(num - 1);
// move the tower's number bigger than 2
hanoiField[findNumPlace(num)[1]][findNumPlace(num)[0]] = 0;
to = x - (lastFoundLine + lastMovedLine);
hanoiField[(y - 1) - countLine(to)][to] = num;
lastMovedLine = to;
// shift place when the tower's number is even.
if (num % 2 == 0) {
// System.out.println("!!!!!");
lastMovedLine = lastFoundLine;
}
// move 1, 2 again
moveTower(num - 1);
}
}
public String blank(int num) {
String s = String.valueOf(this.y);
int l = s.length();
String o = " ";
for (int i = l - num; i > 0; i--) {
o = o + " ";
}
return o;
}
}
package hanoiTower;
public class Driver {
public static void main(String[] args) {
// You can change the start height of HanoiTower.
Hanoi h = new Hanoi(5);
h.initHanoiTower();
}
}