티스토리 뷰

It

JSP DB에 값 저장 불러오기

whyuus 2022. 12. 11. 22:16

JSP DB에 값 저장 불러오기

JSP DB에 값 저장 불러오기

JSP DB에 값 저장 불러오기

만들것들

-- 두수 입력받는 inputXY.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

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

x : <input type="text" name="x">

y : <input type="text" name="y">

<input type="submit" value="저장">

</form>

</body>

</html>

---- 입력받은 두수를 DB로 삽입하는 insertDB.jsp (DB값 불러오기도 구현)

<%@ 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 x = request.getParameter("x");

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

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();

// 입력받은값 저장

stmt.executeUpdate("insert into inputXY (x,y) values('"+x+"','"+y+"');");

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

ResultSet rs = stmt.executeQuery("select * from inputXY");

//객체의 값이 있으면 TRUE

while(rs.next()){

int xx= rs.getInt("x");

int yy= rs.getInt("y");

out.println("<br> x : "+xx+"<br> y : "+yy);

}

}finally{

try{

stmt.close();

}catch(Exception ignored){

}

try{

conn.close();

}catch(Exception ignored){

}

}

%>

</body>

</html>

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함