안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2011년 7월 22일 금요일

두 객체가 같다

 1) 두 레퍼런스가 똑같은 객체를 가리킴 (Reference Equality)
 2) 제목이 똑같은 서로 다른 객체 (Object Equality)

JAVA GUI Layout Manager(레이아웃 메니져)

1) Border Layout => North, South, West, East, Center 다섯 구역에 컴포넌트를 올릴 수 있다.
 Frame의 기본 Layout
2) Flow Layout => 워드 프로세스와 비슷한 방식을 사용한다. 크기는 요구대로 만들 수 있고, 순서대로 '왼쪽 맞춤' 형태로 제공된다. -> 수평 방향으로 들어가지 않을 시에 다음 '행'에 배치 된다.
 Panel 의 기본 Layout
3) Box Layout => 요청 크기대로 만들고 추가된 순서대로 배치한다. 주로 수직 방향으로 쌓을 시에 사용한다. (수평 방향도 가능하지만 잘 사용하지 않음)

JAVA GUI에 뭔가를 넣는 방법

 1) 프레임에 위젯 넣기
 2) 위젯에 2D 그래픽 그리기 => 그래픽 객체를 이용
 3) 그림 넣기

 위 3가지 방법이 있다.

JAVA GUI 중 클릭에 대하여

클릭 시 반응을 얻기 위해서 필요한 두가지

 1) 사용자 클릭시 호출 할 메소드(버튼 클릭 시 행동)
 2) 버튼 클릭을 알 수 있는 방법

 이 두 가지가 필요하다.

 Call Back Method => 이벤트가 일어 났을 때 호출 하는 메소드를 뜻하고 이 메소드가 이벤트 리스너에 그 이벤트가 일어났음을 알린다.
 Event Source => 사용자의 행동을 이벤트로 바꿔주는 객체이다.

2011년 7월 20일 수요일

JDBC select Example(JDBC select 예제)

/*
Database Programming with JDBC and Java, Second Edition
By George Reese
ISBN: 1-56592-616-1

Publisher: O'Reilly
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Example 3.1.
 */
public class Select {
  public static void main(String args[]) {
    String url = "jdbc:msql://carthage.imaginary.com/ora";
    Connection con = null;

    try {
      String driver = "com.imaginary.sql.msql.MsqlDriver";

      Class.forName(driver).newInstance();
    catch (Exception e) {
      System.out.println("Failed to load mSQL driver.");
      return;
    }
    try {
      con = DriverManager.getConnection(url, "borg""");
      Statement select = con.createStatement();
      ResultSet result = select
          .executeQuery("SELECT test_id, test_val FROM test");

      System.out.println("Got results:");
      while (result.next()) { // process results one row at a time
        int key = result.getInt(1);
        String val = result.getString(2);

        System.out.println("key = " + key);
        System.out.println("val = " + val);
      }
    catch (Exception e) {
      e.printStackTrace();
    finally {
      if (con != null) {
        try {
          con.close();
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
}


출처: http://www.java2s.com/Code/Java/Database-SQL-JDBC/JDBCselect.htm

JDBC getConnection

public static Connection getConnection() {

try {
Class.forName("org.gjt.mm.mysql.Driver"); // Class.forName("myDriver.ClassName");
// ?

} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url, userid, password);

} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}

return con;
}

JDBC Insert Example(JDBC insert 예제)


public static void insertEmployees()
 {
  Connection con = getConnection();
  
  String insertString1, insertString2, insertString3, insertString4;
  insertString1 = "insert into Employees values(6323, 'Hemanth')";
  insertString2 = "insert into Employees values(5768, 'Bob')";
  insertString3 = "insert into Employees values(1234, 'Shawn')";
  insertString4 = "insert into Employees values(5678, 'Michaels')";
  

  try {
   stmt = con.createStatement();
      stmt.executeUpdate(insertString1);
      stmt.executeUpdate(insertString2);
      stmt.executeUpdate(insertString3);
      stmt.executeUpdate(insertString4);

   stmt.close();
   con.close();

  } catch(SQLException ex) {
   System.err.println("SQLException: " + ex.getMessage());
  }