티스토리 뷰
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>
'It' 카테고리의 다른 글
PHP 개요 및 설치 , echo문, 변수선언 (0) | 2022.12.13 |
---|---|
JSP DB정보 검색하기 (입력한값 갖고오기 / DB검색) (0) | 2022.12.12 |
중국의 신석기 문화 (0) | 2022.12.10 |
MEGA FTA (다수의 협상국이 참여하는 무역자유화협정) (0) | 2022.12.09 |
도시생태계 (0) | 2022.12.08 |