250 likes | 533 Vues
Database Programming using JSP and MySQL. 2003.4.13 Byung-Hyun Ha bhha@pusan.ac.kr. Table of Contents. Database Application Overview of Application Building Database Access via Telnet File System Access via FTP First JSP Programming Database Programming using JSP Discussion.
E N D
Database Programmingusing JSP and MySQL 2003.4.13 Byung-Hyun Ha bhha@pusan.ac.kr
Table of Contents • Database Application • Overview of Application Building • Database Access via Telnet • File System Access via FTP • First JSP Programming • Database Programming using JSP • Discussion
Database Application • Multi-tier architecture back-end front-end Network business logic + presentation layer DB back-end front-end Network presentation layer business logic DB
Our Database Application • 게시판 back-end front-end Network business logic + presentation layer DB Web browser Internet JSP page MySQL
Back-End Architecture • Powered by linux ike.ie.pusan.ac.kr (linux machine) Apache Tomcat (Application Server) MySQL (DBMS) Internet JSP page (program) Database
Overview of Application Building • Telnet + FTP + MySQL console ike.ie.pusan.ac.kr Telnet daemon shell MySQL Database Internet file system Tomcat FTP daemon JSP page (program)
Database Access via Telnet • Login • 시작 실행 “telnet ike.ie.pusan.ac.kr” • ID와 password 입력 • Password의 수정 • passwd • Logout • exit • 다시 login
Database Access via Telnet • Database console에 login • mysql -u uXXXXX -p • Password 입력 • Database console에서 logout • quit • Password의 수정 • mysqladmin -u uXXXXX –p password ‘YYYYYYYY’ • Console에 다시 login
Database Access via Telnet • 사용 가능한 database 보기 • show databases; • Database 사용 • use dXXXXX • Table 생성 • create table test(no int, name varchar(20)); • Table 확인 • show tables; • Record 입력 • insert into test values(10, ‘abc’); • 입력된 내용 확인 • select * from test;
Database Access via Telnet • 지금 한 것 ike.ie.pusan.ac.kr Telnet daemon shell MySQL Database Internet file system Tomcat FTP daemon JSP page (program)
File System Access via FTP • 테스트 Web page 작성 • 시작 실행 “notepad” • 다음을 입력 • 바탕 화면에 ‘test.html’로 저장 <html> <body> <h1>Hello! HTML!</h1> </body> </html>
File System Access via FTP • Internet Explorer 실행 • 주소 창에 다음을 입력 • ftp://ike.ie.pusan.ac.kr/webapps/ • 파일 다른 이름으로 로그인 • 자신의 ike.ie.pusan.ac.kr 계정을 사용하여 로그인 • ‘test.html’을 Internet Explorer에 drag & drop • 새로운 Internet Explorer 실행 • http://ike.ie.pusan.ac.kr:8080/uXXXXX/test.html
File System Access via FTP • 지금 한 것 ike.ie.pusan.ac.kr Telnet daemon shell MySQL Database Internet file system Tomcat FTP daemon JSP page (program)
File System Access via FTP • MySQL을 종료함 • 현재 경로의 내용확인 • ls • ‘test.html’ 확인 • cd webapps • ls
Our First JSP Programming • 테스트 JSP page 작성 • notepad를 열어 다음을 입력하고, ‘test.jsp’로 저장 <html> <body> <h1><%= "Hello, JSP! " %></h1> <% String str = request.getParameter("name"); if (str != null) { out.println("You have input " + str); } %> <hr/> <form name="form1" method="post" action="test.jsp"> Your input? <input type="text" name="name" /> <input type="submit" value="OK" /> </form> </body> </html>
Our First JSP Programming • ‘test.jsp’를 ike로 전송 • 결과 확인
Our First JSP Programming • 만일 오류가 있다면?
Database Programming using JSP • ‘list.jsp’의 작성 <%@page import="java.sql.*"%> <html><body> <% try { String dbUrl = "jdbc:mysql://localhost/dXXXXX?user=uXXXXX&password=YYYYYY"; Connection conn = DriverManager.getConnection(dbUrl); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM test"); while (rs.next()) { int no = rs.getInt("no"); String name = rs.getString("name"); out.println(no + ", " + name + "<br/>"); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { out.println(e); } %> <p><a href="insert.jsp">insert</a></p> </body></html>
Database Programming using JSP • ‘insert.jsp’의 작성 <%@page import="java.sql.*"%> <html><body> <% String no = request.getParameter("no"); String name = request.getParameter("name"); if (no != null && name != null) { try { String dbUrl = "jdbc:mysql://localhost/dXXXXX?user=uXXXXX&password=YYYYYY"; Connection conn = DriverManager.getConnection(dbUrl); Statement stmt = conn.createStatement(); String query = "INSERT INTO test (no, name) VALUES (" + no + ", '" + name + "')"; stmt.execute(query); stmt.close(); conn.close(); out.println("one row inserted."); } catch (Exception e) { out.println(e); } } %> <form name="form1" method="post" action="insert.jsp"> <input type="text" name="no" /> <input type="text" name="name" /> <input type="submit" value="OK" /> </form> <p><a href="list.jsp">list</a></p> </body></html>
Overview of Application Building • Telnet + FTP + MySQL console ike.ie.pusan.ac.kr Telnet daemon shell MySQL Database Internet file system Tomcat FTP daemon JSP page (program)
Database Application • Multi-tier architecture back-end front-end Network business logic + presentation layer DB back-end front-end Network presentation layer business logic DB
Discussion • HTML vs. JSP • 한글? • 숙제의 의미?