StartIT

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

Server/centos

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

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

//input.jsp//

<%@ page contentType="text/html; charset=utf-8" %>

<HTML>

<HEAD></HEAD>

<BODY>

<FORM action="save_do.jsp" method="post">

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

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

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

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

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

</FORM>

</BODY>

</HTML>

//save_do.jsp//

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

<%

request.setCharacterEncoding("utf-8");

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

String usr = "유저명";

String pwd = "password";

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

Connection con = null;

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

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

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

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

int age=0;

if(!age1.equals("")) = Integer.parseInt(age1);

try

{

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

String sql = "INSERT INTO 테이블명(컬럼명,컬럼명...) VALUES(?,?,?,?)";

PreparedStatement pstmt = con.prepareStatement(sql);

pstmt.setString(1,name);

pstmt.setInt(2,age);

pstmt.setString(3,email);

pstmt.setString(4,phone);

pstmt.executeUpdate();

pstmt.close();

con.close();

}

catch(SQLException e)

{

out.println(e);

}

response.sendRedirect("list.jsp");

%>

 

 

참고:

https://hahahia.tistory.com/94?category=446168

 

JSP, DB연동을 이용한 회원리스트 구현(회원추가)

/* input.jsp */ /* 앞에서 목록생성을 한 list에서 저장링크를 누르면 나오는 페이지입니다(회원가입)*/ <%@ page contentType="text/html;charset=utf-8" %> ID : 이름 : PW : /* save_do.jsp */ <%@ page cont..

hahahia.tistory.com

 

반응형
Comments