StartIT

jsp, postgresql 연동으로 db를 tomcat서버에서 접근하기(update) 본문

Server/centos

jsp, postgresql 연동으로 db를 tomcat서버에서 접근하기(update)

IT스타터 2020. 6. 29. 22:46
반응형

//modify.jsp//

<%@page contentType="text/html;charset=utf-8" import="java.sql.*" %>

<%

request.setCharacterEncoding("utf-8");

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

String url = "jdbc:postgresql://서버주소/디비명";

String usr = "유저명";

String pwd = "password";

String name="",email="",phone="";

int age=0;

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

try

{

Class.forName("org.postgresql.Driver");

con = DriverManager.getConnection(url, usr, pwd);

stmt = con.createStatement();

String query="select * from 테이블명 where num = " + num;

rs = stmt.executeQuery(query);

while(rs.next()){

name = rs.getString("name");

email = rs.getString("email");

phone = rs.getString("phone");

age = rs.getInt("age");

}

%>

<HTML>

<HEAD></HEAD>

<BODY>

<FORM action="modify_do.jsp?num=<%=num%>" method="post">

이름: <INPUT type="text" name="name" maxlength="3" size="3" value="<%=name%>"><br>

나이: <INPUT type="text" onKeyUp="this.value=this.value.replace(/[^0-9]/g.'')" name="age" maxlength="2" size="2" value="<%=age%>"><br>

e-mail 주소: <INPUT type="text" name="email" size="24" value="<%=email%>"><br>

전화번호: <INPUT type="text" name="phone" maxlength="14" size="14" value="<%=phone%>"><br>

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

<INPUT type="submit" value="취소" onclick="javascript: form.action='list.jsp';">

</FORM>

</BODY>

</HTML>

<%

rs.close();

stmt.close();

con.close();

}

catch (SQLException e){

out.println("err:"+e.toString());

}

%>

//modify_do.jsp//

<%@page contentType="text/html;charset=utf-8" import="java.sql.*" %>

<%

request.setCharacterEncoding("utf-8");

String url = "jdbc:postgresql://서버주소/디비명";

String usr = "유저명";

String pwd = "password";

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

String age1=request.getParameter("age");

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

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

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

int num = Integer.parseInt(num1);

int age = Integer.parseInt(age1);

Connection con = null;

try

{

con = DriverManager.getConnection(url, usr, pwd);

String sql = "UPDATE 테이블명 set 칼럼명=?, 칼럼명=?, 칼럼명=?, 칼럼명=? where 칼럼명 = ?";

PreparedStatement pstmt = con.prepareStatement(sql);

pstmt.setString(1,name);

pstmt.setInt(2,age);

pstmt.setString(3,email);

pstmt.setString(4,phone);

pstmt.setInt(5,num);

pstmt.executeUpdate();

pstmt.close();

con.close();

}

catch(SQLException e)

{

out.println(e);

}

response.sendRedirect(list.jsp");

%>

 

반응형
Comments