public class First {
public static void main(String[] args) {
System.out.println("Hello World");
First main = new First();
main.hello();
}
public void hello() {
System.out.println("Second Hello World");
}
}
int num = 0;
String str = "はじめの一歩";
First main = new FIrst();
List<String> list = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
Random rnd = new Random();
public class H2Dao {
private final String DB_URI = "jdbc:h2:/Users/takk/Java/jars/h2/db_file/test.mv.db";
private Connection con;
public H2Dao() throws SQLException {
try {
// DriverManager.getConnection(URI , ユーザー名, パスワード(なし))
con = DriverManager.getConnection(DB_URI, "sa", "");
Statement stmt = con.createStatement();
createTables(stmt);
ResultSet result = stmt.executeQuery("select * from test;");
while(result.next()) {
int id = result.getInt(1);
String name = result.getString(2);
System.out.println("ID: " + id + " Name: " + name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
}
public void createTables(Statement stmt) throws SQLException {
String sql = "DROP TABLE IF EXISTS TEST;\n" +
"CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255));\n" +
"INSERT INTO TEST VALUES(1, 'Hello');\n" +
"INSERT INTO TEST VALUES(2, 'World');\n" +
"SELECT * FROM TEST ORDER BY ID;\n" +
"UPDATE TEST SET NAME='Hi' WHERE ID=1;\n" +
"DELETE FROM TEST WHERE ID=2;";
stmt.execute(sql);
}
public void finalize() {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
con = null;
}
}
}
そして、このクラスを動かします。今回は、JUnitを使用します。
public class H2DaoTest {
@Test
public void testConstractor() {
try {
H2Dao dao = new H2Dao();
} catch (SQLException e) {
fail("エラーになりました");
}
}
}
public class StringUtils {
/** 定数 */
public static final int EMPTY = 0;
public static final int EQUAL = 1;
public static final int IGNORE = 2;
public static final int ERROR = -1;
public int stringTest01(String str1, String str2) {
if ("".equals(str1)) {
return EMPTY;
}
if ("".equals(str2)) {
return EMPTY;
}
return ERROR;
}
public int stringUtils02(String str1, String str2) {
int result = ERROR;
// if (this.isEmpty(str1, str2)) {
// return ERROR;
// }
if (str1.equals(str2)) {
result = EQUAL;
} else {
result = ERROR;
}
return result;
}
public boolean isEmpty(String str1, String str2) {
if (str1 == null || str2 == null) {
return true;
}
if ("".equals(str1) || "".equals(str2)) {
return true;
}
return false;
}
}
< StringUtilsTest>
public class StringUtilsTest {
@Test
public void testStringUtils01() {
StringUtils target = new StringUtils();
// 作成するメソッドの<処理>の1の内容を確認する処理
// assertEquals(想定する結果の値, 検証する値);
assertEquals(StringUtils.EMPTY, target.stringTest01("", null));
}
@Test
public void testStringUtils02() {
StringUtils target = new StringUtils();
assertEquals(StringUtils.EQUAL, target.stringUtils02("aaa", "aaa"));
assertEquals(StringUtils.ERROR, target.stringUtils02("aaa", "aab"));
//assertEquals(StringUtils.ERROR, target.stringUtils02(null, "aab"));
assertEquals(StringUtils.IGNORE, target.stringUtils02("aaa", "AAA"));
}
}
public static void main(String[] args) {
Character[] party = new Character[2];
party[0] = new Hero("太郎");
party[1] = new Wizard("二郎");
System.out.println("こんにちは、良いパーティですね。");
for (int i = 0; i < party.length; i++) {
// ここの処理を変更する必要がある
System.out.println(party[i].getName() + "さん");
}
}