aspose file tools*
The moose likes JDBC and the fly likes Problem in inserting record to Oracle Database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem in inserting record to Oracle Database." Watch "Problem in inserting record to Oracle Database." New topic
Author

Problem in inserting record to Oracle Database.

Radhika Reddy
Ranch Hand

Joined: Oct 31, 2002
Posts: 30
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
rom chatterjee
Ranch Hand

Joined: Dec 11, 2001
Posts: 46
Sounds like the servlet container, try putting it in a Bean and getting the servlet to use the bean


Rom Chatterjee<BR>Sun Certified Java Programmer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in inserting record to Oracle Database.