• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in inserting record to Oracle Database.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to insert record into oracle database from servlet.
Here is the code..
// JPServlet.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JPServlet extends HttpServlet {

/** Initializes the servlet.
*/
Connection con = null;
String sDriver = "oracle.jdbc.driver.OracleDriver";
String sURL="jdbc racle:thin:@your-us67pi6luv:1521:sai";
String sUsername = "scott";
String sPassword = "tiger" ;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
//try to load oracle driver
Class.forName(sDriver).newInstance();
System.out.println("Loading JDBC DRiver is successful");
}catch(Exception e)
{
System.err.println("Failed to Load Driver");
return;
}
try
{
con = DriverManager.getConnection ( sURL,
sUsername,
sPassword);
System.out.println("Obtaining connection is successful");
}
catch ( Exception e)
{
System.err.println( "problems connecting to " +
sURL + ":" );
System.err.println( e.getMessage() );
if( con != null)
{
try { con.close(); }
catch( Exception e2 ) {}
}
return;
} // end catch

}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

int iRowCount = 0;
Statement stmt = null
try
{
stmt = con.createStatement();
System.out.println("Statement creation is successful");
iRowCount = stmt.executeUpdate("insert into job_post values(111,'pppp','mm','cc','cccccoding',sysdate)");
System.out.println( iRowCount +
" Rows inserted into JPData.");
}
catch ( Exception e )
{
System.err.println(
"problem with SQL sent to " + sURL + ":" );
System.err.println( e.getMessage() );
}
finally
{
try { stmt.close(); }
catch( Exception e ) {}
try { con.close(); }
catch( Exception e ) {}
} // end finally clause
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}

}
when I execute it(http://loaclhost:8080/devel/servlet/JPServlet) using Tomcat4.1.12 the error on serverside is " Problem with SQL sent to jdbc racle:thin:your-us67pi6luv:1521:sai:null"
when I wrote same code in java main program it is working fine and I am able to insert record into oracle Database..
This is my autoxec.bat
set JAVA_HOME=C:\j2sdk1.4.0_03
set J2EE_HOME=C:\j2sdkee1.3.1
set CATALINA_HOME=C:\Tomcat 4.1
set ANT_HOME=C:\ant
set PATH=C:\j2sdk1.4.0_03\bin;C:\j2sdkee1.3.1\bin;C:\ant\bin;C:\oracle\ora81\bin;%PATH%;
set CLASSPATH=.;C:\devel;C:\Tomcat 4.1\common\lib\servlet.jar;C:\oracle\ora81\jdbc\lib\classes12.zip;C:\oracle\ora81\jdbc\lib\nls_charset12.zip;%CLASSPATH%;
I can't figure out why its not working in servlet..need help..
Regards
Radhika
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like the servlet container, try putting it in a Bean and getting the servlet to use the bean
reply
    Bookmark Topic Watch Topic
  • New Topic