완전초보ㅠ
92
2020-11-29 22:05:45
3
217

jsp 조언 좀 여쭙겠습니다...ㅠ


SchoolBoard_output-box.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*, java.util.*" %>
    <% request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>게시판 내용 보기</title>
	<script type="text/javascript">
 
    function deletePage(num){
        if(confirm("글이 삭제됩니다.")==true){
            location.href="delete.jsp?num="+num;
        }
    };
    
    function updatePage(num){
        if(confirm("글을 수정하시겠습니까?.")==true){
            location.href="update.jsp?num="+num;
        }
    };
    
    
</script>
</head>
<body>
	<%
	String num = request.getParameter("num");
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/dbmember";
	Connection conn = 
			DriverManager.getConnection(url,"member","member");
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	try{
		String strSQL = "SELECT*FROM SchoolBoard WHERE num=?";
		pstmt = conn.prepareStatement(strSQL);
		pstmt.setInt(1, Integer.parseInt(num));
		rs = pstmt.executeQuery();
		rs.next();
		String name = rs.getString("name");
		String school = rs.getString("school");
		String title = rs.getString("title");
		String contents = rs.getString("contents").trim();
		contents=contents.replaceAll("\n","<br>");
		String writedate = rs.getString("writedate");
		int readcount=rs.getInt("readcount");
	%>
	<center><font size='3'><b> - 학교 게시판 - </b></font>
	<TABLE border='0' width='600' cellpadding='0' cellspacing='0'>
		<TR>
			<TD><hr size='1' noshade></TD>
		</TR>
	</TABLE>
	
	<TABLE width='600' border='0'>
		<TR bgcolor='ededed'>
			<TD align='left'><font size='2'>작성자 : <%=name %></font></TD>
			<TD align='left'><font size='2'> / &nbsp;학교 명 : <%=school %></font></TD>
			<TD align='right'><font size='2'>작성일: <%=writedate %>, 조회수 :<%=readcount %></font></TD>
		</TR>
	</TABLE>
	<TABLE border='0' cellspacing='3' cellpadding='3' width='600'>
		<TR class='board-item'>
			<TD algin=center><font size='3'><b><%=title %></b></font></TD>
		</TR>
	</TABLE>
	
	<TABLE border='0' cellspacing='5' cellpadding='10' width='600'>
		<TR bgcolor='ffffff'>
			<TD> <font size='2' color=''><%=contents %></font></TD>
		</TR>
	</TABLE>
	<TABLE border='0' width='600'>
		<TR>
			<TD><br></TD>
		</TR>
	</TABLE>
	<TABLE border='0' width='600' cellpadding='0' cellspacing='0'>
		<TR>
			<TD><hr size='1' noshade></TD>
		</TR>
	</TABLE>
	
	<TABLE border='0' width='600'>
		<TR>
			<TD align='right'>
				<button type=button onClick="location.href='update.jsp?num=<%=num%>'">수정</button>
				<button onClick="deletePage(<%=num %>)">[삭제]</button>
				<a href='SchoolBoard.jsp'>[목록보기]</a>
			</TD>
		</TR>
	</TABLE>
	<%
	strSQL = "UPDATE tblboard SET readcount = readcount+1 WHERE num = ?";
			pstmt = conn.prepareStatement(strSQL);
			pstmt.setInt(1,Integer.parseInt(num));
			pstmt.executeUpdate();
	}catch(SQLException e){
		out.print("SQL에러 "+e.toString());
	}catch(Exception ex){
		out.print("JSP에러 "+ex.toString());
	}finally{
		rs.close();
		pstmt.close();
		conn.close();
	}
	%>
</body>
</html>


update.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*, java.util.*" %>
    <% request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>수정 페이지</title>
<SCRIPT language="javascript">
		function Check(){
			if(update.name.value.length < 1){
				alert("작성자 이름을 입력하세요.");
				update.name.focus();
				return false;
			}
			if(update.school.value.length < 1){
				alert("학교 명을 입력하세요.");
				update.school.title.focus();
				return false;
			}
			
			if(update.title.value.length < 1){
				alert("글 제목을 입력하세요.");
				update.title.focus();
				return false;
			}
			if(update.contents.value.length < 1){
				alert("글 내용을 입력하세요.");
				update.contents.focus();
				return false;
			}
			
		}
	</SCRIPT>
</head>
<body>
<%
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/dbmember";
    String mysql_id = "member";
    String mysql_pw = "member";

    String num = request.getParameter("num");

    try{
      Connection conn = DriverManager.getConnection(url, mysql_id, mysql_pw);
      Statement stmt = conn.createStatement();
      String sql = "select name,school,title,contents from SchoolBoard where num='?'"+num;  
      ResultSet rs = stmt.executeQuery(sql);

      if(rs.next()){
    	String name = rs.getString(1);
    	String school = rs.getString(2);
        String title = rs.getString(3);
        String contents = rs.getString(4);
  %>
	
<form name='Update' Action='UpdateAction.jsp' Method='post' OnSubmit='return Check()'>
			<table border='0' width='700' cellpadding='2' cellspacing='2'>
			<tr>
					<td colspan='2'>
					 	<hr size='1' noshade>
					</td>
				</tr>
				<tr>
					<td width='200' bgcolor='#2E3091'>
						<font size='2' color='white'><center><b>작성자</b></center></font>
					</td>
					<td>
						<p><input type='text' size='12' name='name' value='<%=name%>'> <font size='1'>* 필수</font></p>
					</td>
				</tr>
				
				<tr>
					<td width='200' bgcolor='#2E3091'>
						<font size='2' color='white'><center><b>비밀번호</b></center></font>
					</td>
				</tr>
				<tr>
					<td width='200' bgcolor='#2E3091'>
						<font size='2' color='white'><center><b>학교 명</b></center></font>
					</td>
					<td>
						<p><input type='text' size='12' name='school' value='<%=school%>'> <font size='1'>* 필수</font></p>
					</td>
				</tr>
				<tr>
					<td colspan='2'>
					 	<hr size='1' noshade>
					</td>
				</tr>
				<tr>
					<td width='200' bgcolor='#2E3091'>
						<font size='2' color='white'><center><b>글 제목</b></center></font>
					</td>
					<td>
						<font size='2'>
							<input type='text' size='70' maxlength='50' name='title' value='<%=title%>'>
						</font>
					</td>
				</tr>
				<tr>
					<td width='200' bgcolor='#2E3091'>
						<font size='2' color='white'><center><b>글 내용</b></center></font>
					</td>
					<td>
						<font size='2'>
							<textarea cols='70' rows='15' wrap='virtual' name='contents'>
							<%=contents %>
							</textarea>
						</font>
					</td>
				</tr>
				
			</table>
			<tr>
				<td align='center' colspan='2' width="100%">
					<table>
						<tr>
							<td width='100' align='center'>
								<input type='reset' value='다시 작성'>
							</td>
							<td width='100' align='center'>
								<input type='submit' value='수정 하기'>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</form>
		 <%

      rs.close();

      stmt.close();

      conn.close();

      }

    }catch(Exception ex){
		out.print("JSP에러 "+ex.toString());
    }

  %>

</body>
</html>


updateAction.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*, java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>수정 페이지</title>
<body>

  <%
    Class.forName("com.mysql.jdbc.Driver"); 
    String url = "jdbc:mysql://localhost:3306/dbmember";
    String mysql_id = "member"; 
    String mysql_pw = "member"; 
    request.setCharacterEncoding("utf-8");

    String num = request.getParameter("num");
    String name = request.getParameter("name");
    String school = request.getParameter("school");
    String title = request.getParameter("title");
    String contents = request.getParameter("contents");





    // 데이터 베이스 연동 후 쿼리 적용 하는 부분

    try{

      Connection conn = DriverManager.getConnection(url, mysql_id, mysql_pw);
      String sql = "update board set name=?,school=?,title=?,contents=? where num=?";
      PreparedStatement pstmt = conn.prepareStatement(sql);

      pstmt.setString(1,name);
      pstmt.setString(2, school);
      pstmt.setString(3, title);
      pstmt.setString(4, contents);
      pstmt.setString(5, num);

      pstmt.execute();

  %>

  <script>
    alert("수정 되었습니다");
    window.location.href = "SchoolBoard.jsp?num="+<%=num%>;
  </script>

  <%
      pstmt.close();
    }catch(Exception ex){
		out.print("JSP에러 "+ex.toString());
    }

  %>
  </body>
  </html>

에러 구문


JSP에러 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3' at line 1 



에러구문 뜻은 찾아서 첫번째 줄 3 근처에 문제가 있다고 해서 아는데

그래도 어디가 틀린지 모르겠네요...ㅠ 첫번째 줄에 3도 없습니다 ㅠ


오류는 SchoolBoard_output-box.jsp라는 게시판 내용 보기 페이지에서 수정을 누르면 주소 창을 보면 update로 넘어는 가는데 실상 보여야 할 내용이 안 보이고 해당 오류 구문이 뜹니다...

0
  • 답변 3

  • 프로야근맨
    260
    2020-11-29 22:39:04 작성 2020-11-29 22:43:09 수정됨

    from SchoolBoard where num='?'"+num; <--- 이 부분이 잘못되어 있는거 같아요 아래 업데이트 쿼리 참고하셔서 맞게 수정해보세요

    글구 에러 보면 sql 문법 에러같은 경우는  String sql = "어쩌구 저쩌구"; 아랫 줄에 System.out.println(sql); 으로 출력하시고

    실제 출력된 sql문을 db에 직접 쿼리를 질의 했을 때 잘 되는지, 동일한 문법 오류가 발생하는지 체크해보시구요

  • dudgh107
    367
    2020-11-30 00:53:26

    음... 에러 로그를 보시면 sql 문법이 잘못되었다고 나옵니다.

    저렇게 명확하게 오류 내용이 나오는데도 질문을 올린걸 보면 에러 로그를 못보시던지 정말 초보인거 같군요.


    ELECT*FROM << 여기 구분이 붙어 있어서 나는거 같네요.
  • sdfsdfefwefwefew
    9
    2020-11-30 05:59:10 작성 2020-11-30 05:59:32 수정됨
    String sql = "select name,school,title,contents from SchoolBoard where num='?'"+num; 
    +num; -> stmt.setInt(1,num);
     
    and
        }catch(Exception ex){
    		out.print("JSP에러 "+ex.toString());
    ->
    }catch(SQLEXception e){e.printStackTrace();}
  • 로그인을 하시면 답변 을 등록할 수 있습니다.