helloWorld12
20
2017-06-20 10:19:29 작성 2017-06-20 10:28:37 수정됨
1
736

JSP_DB연동이 질문있습니다.


환경 

CeontOS 6.9(x64)

jdk1.8.0_131

Apache-tomcat 8.5.15

oracle database 11g release 2


지금 현재 간단한 웹 페이지 구축중에 있습니다.

회원가입 정보 기입 후 ok 버튼을 누르면 화면상으로는 정보는 보여지는데 

내용이 디비로 보내지지가 않습니다.  어느부분이 잘못 된것지 알려주시면 감사하겠습니다. 



-톰캣서버/context.xml

 <Resource 
 	name="jdbc/TestDB" 
 	auth="Container" 
 	type="javax.sql.DataSource"
    maxActive="100" 
    maxIdle="30" 
    maxWait="10000"
    username="scott" 
    password="tiger" 
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:oracle:thin:@localhost:1521:CENTORA"/>

-톰캣서버 /web.xml

<description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

-WEB-INF/web.xml 

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>


-oracle 11g , SQLplus 


- JoinPro.jsp (회원가입 폼에서 받아 처리하는 부분)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="jsp.member.model.MemberBean" %>
<%@ page import="jsp.member.model.MemberDAO" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Join Process</title>
	<link href="join_style.css" rel="stylesheet" style="text/css"/>
</head>
<body>
	<!-- javaBean Action tag -->
	<jsp:useBean id="memberBean" class="jsp.member.model.MemberBean"/>
	<jsp:setProperty property="*" name="memberBean"/>
	
	
	<%
		MemberDAO dao = MemberDAO.getInstance();
		dao.insertMember(memberBean);
	%>
	
	<div id="wrap">
		<br><br>
		<b><font size="5" color="gray">check your Info</font></b>
		<br><br>
		<font color="blue"><%=memberBean.getName() %></font> , Congraturagation! 
		<br><br>
		
		<table>
			<tr>
				<td id="title">ID</td>
				<td><%=memberBean.getId()%></td>
			</tr>
			<tr>
				<td id="title">PASSWORD</td>
				<td><%=memberBean.getPw() %></td>
			</tr>
			<tr>
				<td id="title">NAME</td>
				<td><%=memberBean.getName() %></td>
			</tr>
			<tr>
				<td id="title">GENDER</td>
				<td><%=memberBean.getGender() %></td>
			</tr>
			<tr>
				<td id="title">BIRTHDAY</td>
				<td>
					<%=memberBean.getBirthyy() %>/
					<%=memberBean.getBirthmm() %>/
					<%=memberBean.getBirthdd() %>/
				</td>
			</tr>
			<tr>
				<td id="title">E-MAIL</td>
				<td><%=memberBean.getMail1() %>@<%=memberBean.getMail2() %></td>
			</tr>
			<tr>
				<td id="title">PHONE NUMBER</td>
				<td><%=memberBean.getPhone() %></td>
			</tr>
			<tr>
				<td id="title">ADDRESS</td>
				<td><%=memberBean.getAddress() %></td>
			</tr>
			
		</table>
		<br>
		<input type="button" value="OK">
	</div>
</body>
</html>


-MemberDAO.java

package jsp.member.model;

import java.sql.*;
/*import javax.naming.*;*/
import jsp.util.DBConnection;

public class MemberDAO {
private static MemberDAO instance;
	
	
	private MemberDAO(){}
	public static MemberDAO getInstance(){
		if(instance==null)
			instance=new MemberDAO();
		return instance;

	}
	
	public Date stringToDate(MemberBean member){
		String year = member.getBirthyy();
		String month = member.getBirthmm();
		String day =member.getBirthdd();
		
		Date birthday=Date.valueOf(year+'-'+month+'-'+day);
		return birthday;
	}//StringToDate End
	
	public void insertMember(MemberBean member) throws SQLException
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		String query = "insert into MEMBER values (?, ?, ?, ?, ?, ?, ?, ?, sysdate)";
		
		try {
			// 커넥션을 가져온다.
			conn = DBConnection.getConnection();
			
			// 자동 커밋을 false로 한다.
			conn.setAutoCommit(false);
			
			/*// 쿼리 생성한다.
			// 가입일의 경우 자동으로 세팅되게 하기 위해 sysdate를 사용
			StringBuffer sql = new StringBuffer();
			sql.append("insert into MEMBER values");
			sql.append("(?, ?, ?, ?, ?, ?, ?, ?, sysdate)");		
			stringToDate(member);
			*/
			
			pstmt = conn.prepareStatement(query);
			pstmt.setString(1, member.getId());
			pstmt.setString(2, member.getPw());
			pstmt.setString(3, member.getName());
			pstmt.setString(4, member.getGender());
			pstmt.setDate(5, stringToDate(member));
			pstmt.setString(6, member.getMail1()+"@"+member.getMail2());
			pstmt.setString(7, member.getPhone());
			pstmt.setString(8, member.getAddress());
			pstmt.executeUpdate();
			conn.commit(); 
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// Connection, PreparedStatement를 닫는다.
			try{
				if ( pstmt != null ){ pstmt.close(); pstmt=null; }
				if ( conn != null ){ conn.close(); conn=null;	}
			}catch(Exception e2){
				e2.printStackTrace();
			}
		} // end try~catch 
	} // end insertMember()
}//class MemberDAO End

-DBConnection.java 

package jsp.util;

import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBConnection {
	public static Connection getConnection() {
		
		Context context = null;
		DataSource dataSource = null;
		Connection connection = null;
		
		try{
			context = new InitialContext();
			dataSource = (DataSource)context.lookup("java:comp/env");
			connection = dataSource.getConnection();
		}catch(Exception e){
			e.printStackTrace();
		}
		return connection;
	}
}


*tomcat-dbcp,jar, ojdbc6.jar 파일 존재하고 있습니다. 

*간단한 소스코드를 통해서 연결 확인했을때는 문제 없었습니다. 



0
0
  • 답변 1

  • siva6
    4k
    2017-06-20 10:30:02

    오류내용이 없어서 그냥 느낌으로 찍으면, JNDI 문제인 것 같네요.
    jdbc/TestDB로 설정했으면 그걸 찾아서 써야 할 것 같은데요.

    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.