미브
20
2020-01-17 23:37:29 작성 2020-01-17 23:43:08 수정됨
2
462

JSP MySQL 질문..


 main.jsp ( 학생 20명이 있고 이름을 클릭하게되면 std_click.jsp 에 학과코드 값을 넘겨 )

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="EUC-KR"> 

<title>Hello World</title>

</head> 

<body>

<H2>학생 목록 

<a href="st_insert.jsp">등록</a>|

        </H2>

        <table width=1000 border=1>

        <tr bgcolor=grape> 

            <th width=50>SEQ</th><th width=150>이름</th><th width=150>학번</th><th width=150>학년</th>

            <th width=150>주민번호</th><th width=150>고등학교</th><th width=150>주소</th><th width=80>학과이름</th>

        </tr> 

 <%

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

try{

String jdbcUrl ="jdbc:mysql://localhost:3306/studentdb?useSSL=false";

String dbId = "root";

String dbPass = "1234";

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(jdbcUrl,dbId,dbPass);

String sql = "select a.SEQ,a.STD_NAME,a.STD_NUMBER,a.DEPARTMENT,a.GRADE,a.SOCIALSECURITYNUMBER,a.HIGHSCHOOL,a.ADDRESS,a.PHONE_NUMBER,b.DPT_NM";

sql +=" from student a, department b ";

sql +=" where a.DEPARTMENT=b.DPT_CD;";

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

while(rs.next())

{

int seq = rs.getInt("SEQ");

String name_s = rs.getString("STD_NAME");

String number_s = rs.getString("STD_NUMBER");

String department = rs.getString("DEPARTMENT");

int grade = rs.getInt("GRADE");

String socialsecuritynumber = rs.getString("SOCIALSECURITYNUMBER");

String highschool = rs.getString("HIGHSCHOOL");

String address = rs.getString("ADDRESS"); 

    String dpt_name = rs.getString("DPT_NM");

%>

<tr>

<td width="100"><%=seq%></td>

        <td width="100"><a href="stdclick.jsp?print=<%=department%>"><%=name_s%></a></td>

        <td width="100"><%=number_s%></td>

        <td width="100"><%=grade%></td>

        <td width="100"><%=socialsecuritynumber%></td>

        <td width="100"><%=highschool%></td>

        <td width="100"><%=address%></td>

        <td width="100"><%=dpt_name%></td>

        </tr>

        

<%}

}catch(Exception e){ 

e.printStackTrace();

}finally{

if(rs != null) try{ rs.close();}catch(SQLException sqle){}

if(pstmt != null) try{ pstmt.close();}catch(SQLException sqle){}

if(conn != null) try{ conn.close();}catch(SQLException sqle){}

}

%>  

        </table>

</body>

</html>

 

 <-- std_click --> 

 <%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<%@ page import="java.sql.*" %> 

<% request.setCharacterEncoding("UTF-8"); %>

<% 

String seqa = request.getParameter("print");


%>

<!DOCTYPE html>

 

<html>

<head>

<meta charset="EUC-KR">

<title>Student Click List</title>

</head>

<body>

전달받은값<%=seqa%>

<table width=1000 border=1>

        <tr bgcolor=grape> 

            <th width=150>이름</th><th width=150>학번</th><th width=150>학과번호</th><th width=150>학년</th>

            <th width=150>주민번호</th><th width=150>고등학교</th><th width=150>주소</th> 

        </tr> 

 

<%

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

try{

String jdbcUrl ="jdbc:mysql://localhost:3306/studentdb?useSSL=false";

String dbId = "root";

String dbPass = "1234";

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(jdbcUrl,dbId,dbPass);

String sql = "select *"+"from student"+"where department = ?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1,seqa);

rs = pstmt.executeQuery();

 

if(rs.next())

{

int seq = rs.getInt("SEQ");

String name_s = rs.getString("STD_NAME");

String number_s = rs.getString("STD_NUMBER");

String department = rs.getString("DEPARTMENT");

int grade = rs.getInt("GRADE");

String socialsecuritynumber = rs.getString("SOCIALSECURITYNUMBER");

String highschool = rs.getString("HIGHSCHOOL");

String address = rs.getString("ADDRESS"); 

%>


<tr>

        <td width="100"><%=name_s%></td>

        <td width="100"><%=number_s%></td>

        <td width="100"><%=department%></td>

        <td width="100"><%=grade%></td>

        <td width="100"><%=socialsecuritynumber%></td>

        <td width="100"><%=highschool%></td>

        <td width="100"><%=address%></td>

        </tr> 

 

<%}

}catch(Exception e){ 

e.printStackTrace();

}finally{

if(rs != null) try{ rs.close();}catch(SQLException sqle){}

if(pstmt != null) try{ pstmt.close();}catch(SQLException sqle){}

if(conn != null) try{ conn.close();}catch(SQLException sqle){}

}

 

%>

</table>

</body>

</html>  


메인 페이지에서 학생의 이름을 클릭하면 그 학생의 학과코드랑 동일한 학생들을 모두 출력하는겁니다.

3시간동안 이것저것 해봤는데 도저히 감을 못 잡겠습니다.. 값은 잘 넘겨지는지 확인 했습니다.


에러 메세지 ( 구글링해도 안 나와요 ..)

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 '= '03'' at line 1




0
  • 답변 2

  • JackJack
    31
    2020-01-18 00:14:16

    SQL syntax error ... MySql에 구문에러..  오타가 난거 같은데 찾아보시면 될 거 같습니다. 

  • 아빠아버지
    1k
    2020-01-18 10:10:15

    String sql = "select *"+"from student"+"where department = ?";

    문자열을 붙이다보면 공백을 깜빡하는 경우가 많아요


    String sql = "select * from student where department = ?";

    이렇게 해 보세요 

    조건절하고 테이블명이 붙어있었네요

  • 로그인을 하시면 답변을 등록할 수 있습니다.