problem with servlets
Hi,
I am trying to insert a row into database using servlets and jdbc.I am including it on the on load event in web.xml. But the row is not getting inserted. caould you please point out what mistake i am doing?
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
publicclass DbInsertextends HttpServlet{
Connection theConnection;
private ServletConfig config;
publicvoid init(ServletConfig config)
throws ServletException{
this.config=config;
}
publicvoid service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try{
//Loading Sun's JDBC ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connect to emaildb Data source
theConnection = DriverManager.getConnection("jdbc:oracle:thin:@11.3.4.26:1521:TEST","test","guthrie");
Statement theStatement=theConnection.createStatement();
String sql ="INSERT INTO temp VALUES (?)";
PreparedStatement pstmt = theConnection.prepareStatement(sql);
pstmt.setString(1 ,"TEST");
pstmt.executeUpdate();
theConnection.commit();
pstmt.close();//Close the result set
theStatement.close();//Close statement
theConnection.close();//Close database Connection
}catch(Exception e){
out.println(e.getMessage());
}
}
publicvoid destroy(){
}
}
i also have included
<servlet>
<servlet-name>DbInsert</servlet-name>
<servlet-class>DbInsert</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
in web.xml.
Thanks!!!
Vivek
The code for the db insert is in the service method which fireas when the servlet is called upon to process a request.
If you want the db insert to happen when the servlet is first loaded (dont know what you are trying to achieve here), then put all that jdbc code inside the init() method.
cheers,
ram.
I am unable to guess why you are creating Statement object as you are working with PreparedStatement. This one should work fine but i would like to advice you that for such data base access and insertion you should go for Java Beans.
You better create a DAO on the server you are working with either tomcat or weblogic etc. And use DAO object instead of hard coded database connection.
Or you may be working with the databse client sometimes that causes problem.
Rest of the stuff, I feel is absolutely fair and it should work without any issues.
hI !
Thanks a lot...I chaged my code to this...
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class DbInsert extends HttpServlet{
Connection theConnection;
private ServletConfig config;
public void init(ServletConfig config)
throws ServletException{
this.config=config;
try{
//Loading Sun's JDBC ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connect to emaildb Data source
theConnection = DriverManager.getConnection("jdbc:oracle:thin:@22.3.4.26:1521:TEST","TEST","guthrie");
Statement theStatement=theConnection.createStatement();
String sql = "INSERT INTO temp VALUES (?)";
PreparedStatement pstmt = theConnection.prepareStatement(sql);
pstmt.setString(1 , "TEST");
pstmt.executeUpdate();
theConnection.commit();
pstmt.close();//Close the result set
theStatement.close();//Close statement
theConnection.close(); //Close database Connection
}catch(Exception e){
}
}
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
}
public void destroy(){
}
}
I am trying to insert a row into a table on strating the webserver....but still am not able do it...what might be teh problem....i ran same set of statements as stand alone java application adn i was able to successfully insert into db.....
THANKS!!!
Regards
Vivek
Probably there is some error andcatch(Exception e){ }swallows it. Put a debug statement in there and check your console/logsram.
Hi, how can I print the error....and which log shud i check...i am currently using iplanet webserver...Thanksvivek
Hi, I hav created an equivalent jsp....can i include and call that on load of web.xml?Thanksvivek
>how can I print the error....and which log shud i check...i am currently >using iplanet webserver...
catch(Exception e){
System.out.println(e);
}
check your server documentation to know iff this is displayed on console or written to some log.
if you have a jsp
catch(Exception e){
out.println(e);
}
will display the error in your browser, but then you have to specifically request that jsp from browser.
ram.
Hi!
Thanks a lot....SHud i also privide servlet mapping in web.xml.....here is my code and web.xml.i hav placed DbInsert in classes folder under web-inf
<web-app>
<servlet>
<servlet-name>DbInsert</servlet-name>
<jsp-file>DbInsert</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DbInsert</servlet-name>
<url-pattern>/DbInsert/*</url-pattern>
</servlet-mapping>
</web-app>
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class DbInsert extends HttpServlet{
Connection theConnection;
private ServletConfig config;
public void init(ServletConfig config)
throws ServletException{
this.config=config;
try{
//Loading Sun's JDBC ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connect to emaildb Data source
theConnection = DriverManager.getConnection("jdbc:oracle:thin:@11.3.4.26:1521:TEST","TEST","guthrie");
String sql = "INSERT INTO temp VALUES (?)";
PreparedStatement pstmt = theConnection.prepareStatement(sql);
pstmt.setString(1 , "TEST");
pstmt.executeUpdate();
theConnection.commit();
pstmt.close();//Close the result set
theConnection.close(); //Close database Connection
}catch(Exception e){
System.out.println(e);
}
}
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
}
public void destroy(){
}
}
anu idea on what im doin wrong...the data doesnt get inderted on starting the webserver :-( mean while ill check if there is log....i hav a error log in admin page....but it doesnt print out any messages....pertaining to this....
Thanks
Vivek
hi ,
Sorry,web.xml i hav given the following and not mentioned as in above post,,,,
<servlet>
<servlet-name>DbInsert</servlet-name>
<servlet-class>DbInsert<servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DbInsert</servlet-name>
<url-pattern>/DbInsert/*</url-pattern>
</servlet-mapping>
Dbinsert is placed in classes folder under web-inf..
Thanks
Vivek
The class should be packaged.
package myPackage;
public class DbInsert{
...........
}
the .class file should be in the following path
WEB-INF/classes/myPackage/DBInsert.class
and your web.xml should have
<servlet-class>myPackage.DbInsert<servlet-class>
ram.
Thanks a lot for your help :-)
but still no rows are getting inserted to the database....
i got this from log
[30/Nov/2005:04:49:24] info ( 4012): vs(https-WALC7CH881.wireless.attws.com)servlet 'DbInsert' class = 'myPackage.DbInsert' loaded in context = '/adc'
is there anything wronmg with my servlet...or shud i configure something else as well?
Regards
Vivek
Hi,
I did out.println and i got the following message :
java.sql.SQLException: No suitable driver
how shud i rectify it...i guess i hav set the classpath and i can rub a standalone application to insert data.....shud i place any jar file inside the webserver....if so where shud i place it?
THANKS A LOT FOR ALL YOUR HELP :-)
Regards
Vivek
try using this driverand put classes12.jar into lib folder of your serveryou will find this jar file in the your oracle server.Class.forName("oracle.jdbc.driver.OracleDriver");
HI,
I have plaed classes12.jar in lib folder. But when I tried to connect to database using it as stand alone application , i used ojdbc14.jar....also which i hav placed it in lib directory.But still i am facing the problem......Any help would be of great use ....thanks a o lot!!!
Regards
Vivek
hi ,I am getting this error now....java.lang.ClassNotFoundException: oracle.jdbc.odbc.JdbcOdbcDriver even though jar files are placed in lib directory...ThanksVivek
THANKS A LOT!!!!Its working now :-) RegardsVivek