SAK }‘ŠÙ

SAK Streets ƒƒCƒ“ƒXƒgƒŠ[ƒg }‘ŠÙEƒJƒtƒF ˆê—— SAK Streets ŒŸõ
| VB | SQL | Perl | PHP | ASP | JSP | C | PC | HP | FAQ | ƒtƒŠ[ƒ\ƒtƒg |

PostgreSQL + ServletAJSP •Ò2 - Servlet JDBC ƒAƒNƒZƒXAgetConnection

¡PostgreSQL + JDBCAJAVA Servlet ƒAƒNƒZƒX

  EƒeƒXƒg—p‚̃e[ƒuƒ‹‚ðì¬‚·‚éB

create table testm (
  key char(8),
  data1 int8,
  data2 int8,
  data3 int8
);

insert into testm values ('a001', 1, 2, 3);
insert into testm values ('b002', 10, 20, 30);
insert into testm values ('c003', 100, 200, 300);

commit;

¡PostgreSQL + JDBCAJAVA Servlet ƒAƒNƒZƒXƒeƒXƒg

  EƒT[ƒuƒŒƒbƒg(JAVA Servlet) ‚©‚çAJDBC ‚Å PostgreSQL ‚ɃAƒNƒZƒX‚·‚é
    ‚ɂ́AŽŸ‚̂悤‚É‚·‚éB

// test_postgresql.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class test_postgresql extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

    // HTML ƒwƒbƒ_o—Í
    PrintWriter rw = res.getWriter();
    rw.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
    rw.println("<HTML>");
    rw.println("<HEAD>");
    rw.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\">");
    rw.println("<TITLE>JAVA Servlet - PostgreSQL JDBC Test</TITLE>");
    rw.println("</HEAD>");
    rw.println("<BODY>");
    rw.println("<P>JAVA Servlet - PostgreSQL JDBC Test</P>");

    try {

      // PostgreSQL JDBC ƒhƒ‰ƒCƒoƒ[ƒh
      Class.forName("org.postgresql.Driver");

      // PostgreSQL JDBC Ú‘±
      String dsn = "jdbc:postgresql://sak-design:5432/sak?user=sak&password=sak";
      Connection cn = DriverManager.getConnection(dsn);

      // PostgreSQL JDBC –â‚¢‡‚킹 SQL ì¬
      String sql = "select * from testm";
      Statement st = cn.createStatement();

      // PostgreSQL JDBC ƒŒƒR[ƒhƒZƒbƒgƒI[ƒvƒ“
      ResultSet rs = st.executeQuery(sql);

      // PostgreSQL JDBC ƒŒƒR[ƒhƒZƒbƒgƒŠ[ƒh
      while (rs.next()) {
        rw.println("<P>" + rs.getString(1) + "</P>");
      }

      // PostgreSQL JDBC ƒŒƒR[ƒhƒZƒbƒgƒNƒ[ƒY
      rs.close();

      // PostgreSQL JDBC ƒXƒe[ƒgƒƒ“ƒgƒNƒ[ƒY
      st.close();

      // PostgreSQL JDBC Ú‘±ƒNƒ[ƒY
      cn.close();

    }
    catch (Exception e) {

      // ƒGƒ‰[ˆ—
      rw.println("<P>error</P>");

    }

    // HTML ƒeƒCƒ‹o—Í
    rw.println("</BODY>");
    rw.println("</HTML>");

  }

}

  EJAVA ƒ\[ƒX‚́AŽŸ‚̂悤‚ɃRƒ“ƒpƒCƒ‹‚µ‚āAtest_postgresql.class ‚ðì¬‚·‚éB

cd /src
javac test_postgresql.java

  EServlet(test_postgresql.class) ‚́A
    /jakarta-tomcat-3.3.1/webapps/sak/WEB-INF/classes/ ‚Ì’†‚É”z’u‚µA
    ƒuƒ‰ƒEƒU‚æ‚èAŽŸ‚Ì URL ‚ÅŽÀs‚·‚éB

http://sak-design:8080/sak/servlet/test_postgresql

¡PostgreSQL •ÒAJAVA ServletAJSP •ÒŽ‘—¿
¡MySQL •ÒŽ‘—¿
¡SQL Šî‘b•ÒŽ‘—¿
¡SQL Šî‘bŽÀ’n•ÒŽ‘—¿
¡SQL ƒ`ƒ…[ƒjƒ“ƒO•ÒŽ‘—¿
¡Oracle PL/SQL •ÒŽ‘—¿
| VB | SQL | Perl | PHP | ASP | JSP | C | PC | HP | FAQ | ƒtƒŠ[ƒ\ƒtƒg |

SAK Streets ƒƒCƒ“ƒXƒgƒŠ[ƒg }‘ŠÙEƒJƒtƒF ˆê—— SAK Streets ŒŸõ



Copyright (C) 1999-2017 Y.SAK