메뉴 건너뛰기

정보게시판

 

 

 

 

 

 

 

--- 정보 입력화면 searchInput.HTML

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<form method="post" action="searchDB.jsp">

검색할 이름 <input type="text" name="name">

<input type="submit" value="검색">

</form>

</body>

</html>

 

 

--입력받은값 DB에서 불러오기 searchDB.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>Insert title here</title>

</head>

<body>

<%

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

 

Connection conn = null

Statement stmt = null

 

try {

//드라이버 연결

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

//jspdb는 DB명 // mysql-> user는 root 비밀번호는 1234

conn = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/jspdb""root""1234");

 

if (conn == null)

throw new Exception("데이터베이스 연결 실패");

//연결된 상태를 stmt

stmt = conn.createStatement();

 

// DB에 들어있는 정보를 가져와서 rs객체로저장 (출력)

ResultSet rs = stmt

.executeQuery("select * from studentt where name='"

+ name + "'");

 

if (!rs.next()) {

out.println("해당하는 정보가 없습니다");

}else{

rs.previous();

}

 

//객체의 값이 있으면 TRUE

while (rs.next()) {

String namee = rs.getString("name");

String num = rs.getString("num");

String grade = rs.getString("grade");

String major = rs.getString("major");

 

out.println("<br> name : " + namee + " num : " + num

" grade : " + grade + " major : " + major);

}

 

 

finally {

}

%>

</body>

</html>

위로