package goodsapp; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GoodsDisplayServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=Windows-31J"); PrintWriter out = response.getWriter(); out.println("<HTML><BODY><TABLE border=1>"); out.println("<TR><TD>商品ID</TD><TD>商品名</TD><TD>商品価格</TD></TR>"); //JDBCに関する設定 String driverClass = "";//JDBCドライバのクラス名 String jdbcURL = "";//JDBCのURL String user = "";//ユーザ名 String passowrd = "";//パスワード try { Class.forName(driverClass); Connection conn = DriverManager.getConnection(jdbcURL,user,passowrd); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from goods "); while(rs.next()){ out.println("<TR><TD>"+rs.getString(1)+"</TD>"); out.println("<TD>"+rs.getString(2)+"</TD>"); out.println("<TD>"+rs.getInt(3)+"</TD></TR>"); } } catch (Exception e) { e.printStackTrace(); } out.println("</TABLE></BODY></HTML>"); } }