본문 바로가기

jsp

jsp와 db연동

반응형

[ex] 직원리스트를 누르면 emp테이블에 있는 모든직원들의
 empno가 출력될 수 있도록 소스코드를 작성하시오.

a.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%> 
<!DOCTYPE html>
<html>
 <head></head>
  <body> 
 <a href="b.jsp">직원리스트</a>
  </body>
</html>
[an]
b.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from emp");
 out.println("<table>");
 while(rs.next()){
  out.println("<tr><td>"+rs.getInt(1)+"</td></tr>");
 }
 out.println("</table>");
%>
</body>
</html>

 


[ex] '회원정보보기'를 클릭하면 그회원의 정보를 화면에 출력하는 소스코드를 작성하시오.

b.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<form action="c.jsp">  
<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from emp");
 out.println("<table>");
 while(rs.next()){
  out.println("<tr><td><input type=radio name=empno value="+rs.getInt(1)+"></td><td>"+rs.getInt

(1)+"</td></tr>");
 }
 out.println("</table>");
%>

<input type="submit" value="회원정보보기">
</form>
</body>
</html>

[an]
c.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from emp where empno="+request.getParameter("empno"));
 if(rs.next()){
  out.println(rs.getInt(1)+"<br>");
  out.println(rs.getString(2)+"<br>");
 }
%>
</body>
</html>

 


[ex] 전송버튼을 클릭하면 회원정보가 member테이블에
 insert되는 소스코드를 작성하시오.

a.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%> 
<!DOCTYPE html>
<html>
 <head></head>
  <body>
 <form action="b.jsp">
  <input type="text" name="id"><br>
  <input type="text" name="pw"><br>
  <input type="text" name="addr"><br>
  <input type="text" name="tel"><br>
  <input type="submit">
 </form>
  
  </body>
</html>

[an]
b.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<%
 String id=request.getParameter("id");
 String pw=request.getParameter("pw");
 String addr=request.getParameter("addr");
 String tel=request.getParameter("tel");

 Connection connection=DBUtil3.getConnection();
 PreparedStatement pstmt=connection.prepareStatement("insert into member values(?,?,?,?)");
 pstmt.setString(1, id);
 pstmt.setString(2, pw);
 pstmt.setString(3, addr);
 pstmt.setString(4, tel);
 
 pstmt.executeUpdate();
%>
</body>
</html>


[ex] member테이블에 있는 모든회원을 출력하는 소스코드를 작성하시오.

[an]
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>

<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from member");
 out.println("<table>");
 while(rs.next()){
  out.println("<tr><td>"+rs.getString(1)+"</td>");
  out.println("<td>"+rs.getString(2)+"</td>");
  out.println("<td>"+rs.getString(3)+"</td>");
  out.println("<td>"+rs.getString(4)+"</td></tr>");
 }
 out.println("</table>");
%>

</body>
</html>

 

[ex]
a.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<form action="b.jsp">  
<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from emp");
 out.println("<table>");
 while(rs.next()){
  out.println("<tr>");
  out.println("<td>");
  out.println("<input type=radio name=ename value="+rs.getString(2)+"></td>");
  out.println("<td>"+rs.getString(2)+"</td>");
  out.println("</tr>");
 }
 out.println("</table>");
%>

<input type="submit" value="회원정보보기">
</form>
</body>
</html>

[an]
b.jsp
=====
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.seoulit.DBUtil3" %> 
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 td { border:2px solid green; }
</style>
</head>
<body>
<%
 Connection connection=DBUtil3.getConnection();
 Statement stmt=connection.createStatement();
 ResultSet rs=stmt.executeQuery("select * from emp where ename='"+request.getParameter("ename")+"'");
 if(rs.next()){
  out.println(rs.getInt("empno")+"<br>");
  out.println(rs.getString("job")+"<br>");
 }
%>
</body>
</html>


 

반응형

'jsp' 카테고리의 다른 글

DB연동 회원가입,삭제  (0) 2013.10.31
DB 연동  (0) 2013.10.30
선언문,request객체의메서드들,화면이동하기  (0) 2013.10.23
jsp 1일차 테스트  (0) 2013.10.22
JSP 첫시작~! 1일차  (0) 2013.10.21