728x90
반응형

자바 프로젝트에서 데이터베이스를 postgreSQL을 이용하는 경우, JDBC를 새로 받는다.

다운로드 사이트 : [http://jdbc.postgresql.org]

JDBC를 이용하여 데이터베이스에 연결하는 방법 예제...

import java.sql.*;
public class Untitled1 {

  public Untitled1() {
  }

  public void test() {

    try {
      String url = "jdbc:postgresql://localhost/mydb";
      String user = "Administrator";
      String password = "000000";

      Class.forName("org.postgresql.Driver");

      Connection con = DriverManager.getConnection(url, user, password);
      Statement stmt = con.createStatement();
      ResultSet rset = stmt.executeQuery(" select city from weather ");

      System.out.println("결과 ==>> ");

      if(!rset.next()) {
        System.out.println("Nothing!");
      } else {
        String show = rset.getString(1);
        System.out.println("->> "+show);
      }

      if(rset != null) rset.close();
      if(stmt != null) stmt.close();
      if(con != null) con.close();
    } catch(Exception e) {
      System.out.println(e.getMessage());
    }

  }
  public static void main(String[] args)  throws Exception {
    Untitled1 untitled11 = new Untitled1();
    untitled11.test();
  }
}


728x90
반응형

+ Recent posts