day04_writeForm.jsp
<%@ 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>게시판 입력 폼</title>
	<style type="text/css">
		table{ border: 1px thin red; border-style: solid; padding:10px 10px 10px 10px;
			 background-color:silver;
			 }
	</style>

</head>
<body>
	<form action="day04_insert.jsp">
		<table>
			<tr>
				<td>글제목</td>
				<td>
					<input type="text" name="title" size="20" required="required">
				</td>
			</tr>
			<tr>
				<td>글내용</td>
				<td>
					<textarea rows="5" name="content"></textarea>
				</td>
			</tr>
			<tr>
				<td>
				<input type="submit" value="저장">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>


day04_insert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>db에 인서트 하기</title>
</head>
<body>
	<%
	//1 . 드라이버 로딩
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
		//2. 연결
			try{
				//test는 database 이름 입니다 
				//mysql안에 database 만드는 명령:
				//c:/>mysqladmin -u root creat test -p
				
				String url="jdbc:mysql://localhost:3306/test";
				String user="root"; // 계정 이름 
				String password="1234"; //계정 비밀번호
				conn = DriverManager.getConnection(url, user, password);
				out.print("연결성공 :"+ conn.toString());
			}catch(Exception e){
				out.print("연결 실패-"+e.getMessage());
			}
			
			/*  create table jsp2(no integer primary key auto_increment,
						title varchar(20),context varchar(100)); */
			
			String sql="insert into jsp2 (title,context) values(?,?);";
			
			PreparedStatement preState = conn.prepareStatement(sql);
			
			String title=request.getParameter("title");//첫번째?
			String context=request.getParameter("context");//두번째?
					
			preState.setString(1,title);//첫번째 ?값을 셋팅하기
			preState.setString(2,context);//두번째 ?값을 셋팅하기
					
			int outcome=-1;		
			outcome=preState.executeUpdate();//insert 문 실행
			if(outcome>0){
				out.print("insert 성공 <br/>");
				out.print("결과"+outcome);
			}
	%>
			db에 insert하고 dayo4_list.jsp로 이동하게 만듭니다.
			<%-- <jsp:forward page="day04_list.jsp"></jsp:forward> --%>
</body>
</html>





day04_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection"  %>    
<%@ page import="java.sql.Statement"  %>
<%@ page import="java.sql.ResultSet"  %>
<%@ page import="java.sql.DriverManager"  %>
<%@ page import="java.sql.PreparedStatement" %>
<!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>db에 jsp2테이블 가져오기</title>
	<%
	//1 . 드라이버 로딩
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
		//2. 연결
			try{
				//test는 database 이름 입니다 
				//mysql안에 database 만드는 명령:
				//c:/>mysqladmin -u root creat test -p
				
				String url="jdbc:mysql://localhost:3306/test";
				String user="root"; // 계정 이름 
				String password="1234"; //계정 비밀번호
				conn = DriverManager.getConnection(url, user, password);
				out.print("연결성공 :"+ conn.toString());
			}catch(Exception e){
				out.print("연결 실패-"+e.getMessage());
			}
		
					
			String query="select * from jsp2;";
			stmt=conn.createStatement();
			
			//db에 명령을 날리면 결과가 옵니다.
			rs=stmt.executeQuery(query);
			
			while(rs.next()){//한칸 아래로 이동
				out.print("<br/>"+"no 컬럼 값:" +rs.getString("no")+"<br/>");
				out.print("<br/>"+"title 컬럼 값:" +rs.getString("title")+"<br/>");
				out.print("<br/>"+"context 컬럼 값:" +rs.getString("context")+"<br/>");
			
			}
			
			rs.close();
			stmt.close();
			conn.close();
		%>
</head>
<body>

</body>
</html>



+ Recent posts