이 포스트에서는 jsp로 데이터를 입력하여 mysql의 테이블에 데이터를 추가합니다.



* write_fom.jsp

    write_form.txt



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Write form Page</title>
</head>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<br> 생성할 아이디와 비밀번호를 입력하세요.<br>
<form name="form1" action="write_DB.jsp" method="post">
<p> ID: <input type="text" name="id"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" name="formbutton1" value="보내기">&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>


좋은 글이면 손가락 눌러 주세요. 힘이 됩니다 ^^ 

로그인이 필요 없습니다.





* write_DB.jsp

write_DB.txt





<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Write Database Page</title>
</head>
<body>
<%@ page import = "java.sql.*, java.util.*" %>
<%
String id = request.getParameter("id");
String password = request.getParameter("password");

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
PreparedStatement pstmt = null;

StringBuffer sql = new StringBuffer();

try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
pstmt = conn.prepareStatement("insert into jsp (id, password) values(?,?) ");
pstmt.setString(1, id);
pstmt.setString(2, password);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {

if(pstmt != null)
try {
pstmt.close();
} catch (Exception e) {}
if(conn != null)
try {
conn.close();
} catch (Exception e) {}
}
%>
ID, Password 가 입력되었습니다.
</body>
</html>




* write_form.jsp 실행 결과 화면





* 아래와 같은 정보를 입력하고 보내기 버튼을 클릭한 화면
ID: facebook55
Password: 9988 





* 데이터 입력 결과를 확인하기 위해 read_DB.jsp 실행 화면



posted by 쪼재