2014-06-16 7 views
0

일부 PostgreSQL 쿼리가 작동하는 JSP 코드를 가져 오려고했습니다. jdbc 드라이버 구성에 문제가 있는지 또는 내 코드에 문제가 있는지 여부에 관계없이 문제가 무엇인지 알 수 없습니다.tomcat7 용 Postgres 드라이버 (커넥터) 구성

다음은 context.xml의 구성입니다.

<Resource name="jdbc/filedb" auth="Container" 
      type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" 
      url="jdbc:postgresql://127.0.0.1:5432/testdb" 
      username="akshay" password="akshay" maxActive="20" maxIdle="10" 
maxWait="-1"/> 

또한 리소스 이름을 알고 싶습니다. 여기에 filedis 테이블 이름을 추가했습니다.

이것은 server.xml의 모양입니다.

<GlobalNamingResources> 

    <Resource name="jdbc/filedb" auth="Container" 
       type="javax.sql.DataSource" 
      username="akshay" 
      password="akshay" 
      driverClassName="org.postgresql.Driver" 
       description="User database that can be updated and saved" 
      url="jdbc:postgresql://127.0.0.1:5432/testdb" 
      maxActive="20" 
      maxIdle="10" 
      maxWait="-1" 
       factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
       pathname="conf/tomcat-users.xml" /> 
    </GlobalNamingResources> 

나는 위의 구성없이 실행하려고 시도했다.

내가

<resource-ref> 
<description>postgreSQL Datasource example</description> 
<res-ref-name>jdbc/filedb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 

을 web.xml에 추가 한 것입니다 이건 내 코드입니다 :

<html> 
<body> 
<form name=f1 method=Post action="http://localhost:8080/filedb1.jsp"> 

<table> 
<td> 
Add Header 1<input type=text name=header1 id=header1> 
</td><td> 
Add Header 2<input type=text name=header2 id=header2> 
</td><td> 
Add Header 3<input type=text name=header3 id=header3> 
</td><td> 
Add Header 4<input type=text name=header4 id=header4> 
</td> 
<tr> 
<td> 
Upload File 1<input type=file name=path1 id=file1></td><td> 
Upload File 2<input type=file name=path2 id=file2></td><td> 
Upload File 3<input type=file name=path3 id=file3></td><td> 
Upload File 4<input type=file name=path4 id=file4></td> 
</tr> 
</table> 
<div align="center"> 
<input type=submit value="SUBMIT" onclick="f1.action='http://localhost:8080/filedb1.jsp';return true;"> 
</div> 
</form> 
<%@ page import="java.sql.*" %> 
<%@ page import="javax.servlet.*" %> 
<%@ page import="javax.servlet.http.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="java.util.*" %> 
<%@ page language="java" session="true" %> 

<% 
    String file1 = request.getParameter("file1"); 
    String file2 = request.getParameter("file2"); 
    String file3 = request.getParameter("file3"); 
    String file4 = request.getParameter("file4"); 
    String header1 = request.getParameter("header1"); 
    String header2 = request.getParameter("header2"); 
    String header3 = request.getParameter("header3"); 
    String header4 = request.getParameter("header4"); 
    java.sql.Connection con; 
    con=null; 

    PreparedStatement psmt=null; 
    PreparedStatement psmt1=null; 
    PreparedStatement psmt2=null; 
    PreparedStatement psmt3=null; 

    try{ 
    Class.forName("org.postgresql.Driver"); 
    con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/testdb","akshay","akshay"); 
    psmt=con.prepareStatement("insert into filedb(id,header,content)"+"values (?,?,?)"); 
    psmt.setInt(1,1); 
    psmt.setString(2,header1); 
    psmt.setString(3,file1); 
    int s = psmt.executeUpdate(); 
    %> <font color="orange"> 
    <% 
    if(s>0) 
    { 
     %><%out.println("File Name :"+file1+" added to database.");%><% 
    } 
    else 
    { 
    %><%out.println("File Name :"+file1+" unsuccessfull attempt while adding to database.");%><% 
    } 
    psmt1=con.prepareStatement("insert into filedb(id,header,content)"+"values (?,?,?)"); 
    psmt1.setInt(1,2); 
    psmt1.setString(2,header2); 
    psmt1.setString(3,file2); 
    int s1 = psmt1.executeUpdate(); 
    if(s1>0) 
    { 
     out.println("File Name :"+file2+" added to database."); 
    } 
    else 
    { 
    out.println("File Name :"+file2+" unsuccessfull attempt while adding to database."); 
    } 
    psmt2=con.prepareStatement("insert into filedb(id,header,content)"+"values (?,?,?)"); 
    psmt2.setInt(1,3); 
    psmt2.setString(2,header3); 
    psmt2.setString(3,file3); 
    int s2 = psmt2.executeUpdate(); 
    if(s2>0) 
    { 
     out.println("File Name :"+file3+" added to database."); 
    } 
    else 
    { 
    out.println("File Name :"+file3+" unsuccessfull attempt whileadding to database."); 
    } 
    psmt3=con.prepareStatement("insert into filedb(id,header,content)"+"values (?,?,?)"); 
    psmt3.setInt(1,4); 
    psmt3.setString(2,header4); 
    psmt3.setString(3,file4); 
    int s3 = psmt3.executeUpdate(); 
    if(s3>0) 
    { 
     out.println("File Name :"+file4+" added to database."); 
    } 
    else 
    { 
    out.println("File Name :"+file4+" unsuccessfull attempt whileadding to database."); 
    } 
    psmt1.close(); 
    psmt2.close(); 
    psmt3.close(); 
    psmt.close(); 
    con.commit(); 
    con.close(); 
    } 
    catch(Exception ex) 
    { 
    ex.printStackTrace(); 
    } 

%> 

</font> 

</body> 
</html> 

Table "public.filedb" 
Column | Type | Modifiers 
---------+---------+----------- 
id  | integer | not null 
header | text | not null 
content | text | not null 

filedb 내가 오류를 얻을하지 않습니다 그러나 테이블에는 업데이트가 없습니다. 새로운 기록이 없습니다.

나쁜 들여 쓰기와 내 문제의 사소한 특성에 사과하지만 나는 그것을 능가하지 못했습니다. 어떤 도움을 주셔서 감사합니다.

편집 :

가 나는 문제를 개선 할 수 있도록 내가, 다운 투표 뒤에 이유를 얻을 수 있다면 그것은 나를 위해 도움이 될 것이라고 생각합니다. 또한 jsp에 jdbc가 포함되어서는 안되며 왜 그렇습니까? 그렇다면 jdbc를 사용하기 위해 취할 수있는 대체 조치는 무엇입니까?

+1

** ** JSP에 JDBC 코드를 넣지 마십시오! INSERT 문에서'VALUES' 앞에 공백을 추가하십시오. con.close();를하기 전에'con.commit();'을 추가해보십시오. Postgres 스키마를 포함하도록 편집하십시오. 일반적으로 Postgres는 삽입시 ID를 제공합니다. –

+0

스키마를 만들지 않았습니다. 나는 모든 시정을 기억할 것이다. 변경 후에는 작동하지 않습니다. 구성에 문제가 있습니까? –

+0

'fileddb '라는 테이블을 가진'testdb'를 가지고 있습니까? –

답변

0

그래서 대신 mysql 커넥터를 사용했습니다.

내가

여기
<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/test</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 

내가

<Resource name="jdbc/filedb" auth="Container" type="javax.sql.DataSource" 
maxActive="100" maxIdle="30" maxWait="10000" 
username="akshay" password="akshay" driverClassName="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/> 

을 context.xml에 추가 것입니다 web.xml에 추가 것입니다 그리고 마지막으로이 server.xml의 내부에 무슨 일이 있었는지입니다

<Resource name="jdbc/filedb" auth="Container" 
      type="javax.sql.DataSource" 
      username="akshay" 
      password="akshay" 
      driverClassName="com.mysql.jdbc.Driver" 
      description="User database that can be updated and saved" 
      url="jdbc:mysql://127.0.0.1:3306/test" 
      maxActive="20" 
      maxIdle="10" 
      maxWait="-1" 
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
      pathname="conf/tomcat-users.xml" /> 

나는 postgresql과 작동하지 않는 이유를 모르지만 코드는 실제로 동일하다.