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 + ServletAJSP •Ò2 - Servlet JDBC ƒAƒNƒZƒXAgetConnection

¡PostgreSQL + JDBCAJAVA 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 + JDBCAJAVA 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 ServletAJSP •ÒŽ‘—¿
¡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-2024 Y.SAK