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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt on Servlet having database connection?

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

I have been trying to create a servlet
having a method which creates database
connection and inserts values using SQL statement

My servlet has three methods
doGet();
doPost();
disp();//This method consist of database connection
In the doGet() method i am calling disp();


i tried using the same method disp() in my
simple java program (ex abc.java )it works fine i mean
that it creates connection to the database
as well inserts value in the database table
also.


but when i put the same method disp()
in my Servlet and called it in doGet()
method it does not produce any output
not even a error or exception so i am
totally lost onto what i must do know


past two days i am on the same program
whats the reason that it works fine
in a normal java program but not in my servlet.
Please anyone can help me
on above it will be realy realy great.......


Thanks in advance





Dhawni:>Its always too soon to quit.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi dhwani,

As per your explanation, the code working fine in a normal java program should definitely work well with Servlet as well, since Servlet is also java class.

Can you please paste your java code here? so that it will help to get a solutions quickly!
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hiiii Raghavan...............


this is code for my Servlet





here is my method disp() when i call it in normal java class like abc.java its called
and data gets entered in table userinfo



but the same method when i am calling in doGet() method of my Servlet its not working...............

i hope if can anyone help me to where i am going wrong
it will be realy great.................

thanks in advance

Dhwani:>its always too soon to quit.


[BPSouther: Narrowed the code a bit to make the whole thread easier to read]
[ January 13, 2008: Message edited by: Ben Souther ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi dhwani,

Though you have pasted your code using CodeTags, it would have been better if you took some time to just align/intend it

Well, it works fine for me. As i already told, a Servlet is also a java program only and no different from it, except the way it is treated and maintained.

  • After calling the disp() method, you are redirecting your request to another servlet named "ValidUser" . What exactly are you doing in the doGet() or doPost() method of it?
  • Ideally speaking, you should NOT see the output "welcome to home page" as you are redirecting the output.
  • As an effect of redirection, the request is considered as if a new request from the client and it is sent to the server.




  • Can you check the Server log? As you used SOPs inside disp() method for just try and its NOT with the response's output stream, you may NOT see it in your browser.

    I don't see anything to stop the processing of your disp() method and redirection.

    Just try and let us know with the requested details.
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi Raghavan

    Thanks for your response

    first of all i apologise for code identation mistake

    secondly one more mistake is i forgot to comment that statement
    in the code which i pasted above

    response.redirect("ValidUser");

    actualy in my original code i am NOT redirecting it
    another servlet...the coding is just till
    SOP statement ("Welcome to home page");

    hence i mentioned that i am getting in output
    "welcome to home page" but in my table there is no data
    getting inserted

    You said me to check for Server logs i
    am a beginner in JSP and Servlets so
    i dont have that mush idea onto what i must
    check in my Server logs
    Although there is a foler in Tomcat 6.x named logs
    but it has text docs what i must check than in them?

    do you think there can be any other problem in my code
    that is not allowing my data to enter in table.........
    i am totally lost with program..........



    Any one more doubt onto which book i must refer
    to learn JSP and Servlets so as to get
    more knowledge

    Anyways thanks in advance


    Dhwani:>Its always to soon quit.
     
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by dhwani mathur:
    ...
    first of all i apologise for code identation mistake

    secondly one more mistake is i forgot to comment that statement
    in the code which i pasted above




    You can always go back and edit an existing post by clicking on the
    icon in that post.
    I narrowed the code a bit for you but if there are other errors in there, it
    would be a better idea to edit the post and fix them than to mention the errata later as many people won't see you later posts until after they're read your code.

    -Ben
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by dhwani mathur:

    first of all i apologise for code identation mistake


    That's okay. You will get used to it. But please ensure you follow the same hereafter.

    But as Ben told, you have an option of editing your own post very well. Please keep that in mind for future posts.


    actualy in my original code i am NOT redirecting it
    another servlet...the coding is just till
    SOP statement ("Welcome to home page");



    In that case, your servlet is working without any issues!

    That's the only one output you provide. What else you want to prove further?


    but in my table there is no data
    getting inserted



    This is a different issue. You better try to segregate and locate the error as it really helps all to concentrate!


    You said me to check for Server logs i
    am a beginner in JSP and Servlets so
    i dont have that mush idea onto what i must
    check in my Server logs



    Yes, Tomcat being an application which provides you the runtime environment for running your JSPs and Servlets has its own location to store on what is happening with the applciations it hosts. The way it maintains is through logs.



    Although there is a foler in Tomcat 6.x named logs
    but it has text docs what i must check than in them?



    Yes, you can find out some .log files inside the logs folder. As its been sometime i worked in Tomcat, i don't exactly remember the logfile name.

    I guess you see can a file named "localhost.<yyyy-mm-dd>.log". Check in that file whether have you got any errors.


    Any one more doubt onto which book i must refer
    to learn JSP and Servlets so as to get
    more knowledge



    You can read Marty Hall's Core Servlets and JSPs book which is available for FREE here

    You can also read the famous HFSJ (Head First Servlets & JSP) book by Kathy Seirra, Bert Bates ,Bryan Basham.
     
    Ranch Hand
    Posts: 132
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    does your records inserting in database table?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by Rahul Shilpakar:
    does your records inserting in database table?



    Hi Rahul,

    He says, it [b]is not[/i]. That's where he seems to face a problem!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by Rahul Shilpakar:
    does your records inserting in database table?



    Hi Rahul,

    He says, it is not. That's where he seems to face a problem!
    [ January 14, 2008: Message edited by: Raghavan Muthu ]
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi Raghavan


    first of all let me be very clear i am She not he
    first time ever someone said me he

    anyways rahul yes my data is not getting inserted in my table
    that is where the problem is

    hey raghavan yesterday i checked my server logs it shows
    as below




    although i have created DSN to refer to my database than also
    i am getting exception as Unknown dataSource
    and have specified driver as well
    i wont give up
    i will try more on this program



    anyways thanks a lot
    for all the help till now.

    Dhwani:>Its always too soon to quit.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by dhwani mathur:


    first of all let me be very clear i am She not he
    first time ever someone said me he



    Hello dhwani,

    ouch... Sorry for my ignorance! but your name sounded like you are a he .

    Perhaps i think we can suggest JavaRanch to have an icon to indicate the gender besides the name Wassay?


    hey raghavan yesterday i checked my server logs it shows
    as below





    Yes, that's where we have a catch! It looks like the webserver (tomcat here) is not aware of the datasource being used and thats why it is unable to obtain a connection! That's why you have not got any output at all.

    I have NOT worked with Type1 JDBC-ODBC driver. I have used Type2 and 4 drivers and there we need to provide the .jar file for the JDBC Driver in the lib directory of either your web application or the container's location.

    Let me check with Type1 and get back. Meanwhile other ranchers with experience could help here i guess for Type1 Driver!

    Meanwhile, where have you kept the JDBC driver jar file in your application?


    i wont give up
    i will try more on this program



    That sounds really great! Keep it up!

    Good luck!
    [ January 15, 2008: Message edited by: Raghavan Muthu ]
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi thr

    Thanks for the reply once again
    you said for jdbc.jar file
    i searched it in my pc
    but i am not getting
    it......is the name jdbc.jar?only............

    Dhwani:>Swimmers never weep.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Ouch.. it is not jdbc.jar!

    its the .jar file for your jdbc driver!

    what database you use?
     
    Ranch Hand
    Posts: 225
    Spring Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Try to make proper datasource.
    If you are using oracle,do pass the two additional parameters(username and password) in getConnection method.
    And use proper url:jdbc dbc snname,userid,password

    I think there is no need of additional jar file.
    As Type 1 driver is already available in JDK.


    Regards
    Baseet Ahmed

    "God has made every living thing from water."
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi thr!!!

    i am using MySQL as database
    and and i am checking for .jar file as
    well hope i could find whats actualy missing.....

    i have already checked for proper DSN
    as well..... it is correct....

    and i am using windows authentication
    so no need of username and password.

    i wil get back as i find correct .jar file to
    be included...........for my type 1 driver

    Dhwani:>It alaways too soon to quit.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hello Lady
    Appears like got stuck in the first step of creating a connection, you said it worked fine when you were executing through simple class and not working through a servlet, you sure it is getting inserted in the database, and the mysql*.jar in the tocat version/webapps/your application folder/web-inf/lib folder or the tomcat version/common/lib/ folder both of the tomcat installation directory you should place the jar file.
    if you use a mysql database you might need to use the jdbc dbc://ip ort/database name along with the user name and password for the connection to be created.
    if any doubts check for mysql url sample on google
    thanks
     
    kalyan godavarthy
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hello Lady
    Appears like got stuck in the first step of creating a connection, you said it worked fine when you were executing through simple class and not working through a servlet, you sure it is getting inserted in the database, and the mysql*.jar in the tocat version/webapps/your application folder/web-inf/lib folder or the tomcat version/common/lib/ folder both of the tomcat installation directory you should place the jar file.
    if you use a mysql database you might need to use the jdbc:odbc://ip:port/database name along with the user name and password for the connection to be created.
    if any doubts check for mysql url sample on google
    thanks
     
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Dhwani mathur

    The tip I am gonna give to you is not really related to your question, but still a good tip.
    Please remember whenever you try to use UPDATE or INSERT statement the execution of the query should be like this "stmt.executeUpdate(sql);". what i saw in your code was, "stmt.executeQuery(sql);" which is bad practice, because you will never know what happened after the execution of your query.

    best of luck

    Aneel Ansari
    [ January 16, 2008: Message edited by: Aneel Ansari ]
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hello...........

    as Kalyan Anand said i must put mysql.jar file
    in Tomcat/lib/
    but mysql.jar is to be put when
    i am using third party driver here i am using
    jdbc type 1 driver sun.jdbc.odbc.JdbcOdbcdriver
    so i think there should be no need of
    mysql.jar .......but i am not sure onto if i am right
    or not even i tried to download it but could nt ?
    secondly thanks for the advice aneel ansaree
    i will try to use the right query next time onwards.....

    but my problem is at the same place still i have
    not arrived to a solution onto it.............
    but sure will get solution may be it takes time....


    one thing i checked is when i created
    my DSN i used windows authentication
    and not SQl Server authentication
    ie no username and password so can this
    create any problem in my code?
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    The driver sun.jdbc.odbc.JdbcOdbcdriver won't work for MySql.
    It is for connecting to Microsoft databases via ODBC.

    You will need to have the correct driver for the RDMS that you're using.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    That's very true. You have to go with Type4 Driver for mysql. Please try with that.

    Please read about the MySQL Connector J and its description here.
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Dear dhwani,

    My suggestions for your problem as follows,

    1. copy mysql.jar file on ..\Tomcat_home\..\lib folder

    2. Changes your deriver and approach as follow :

    Class.forName ("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection
    ( url, "your_mysgl_login", "your_mysgl_password" );
    Statement stmt = con.createStatement ();
    ResultSet rs = stmt.executeQuery (query);

    I think above idea will help to you...
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by dhwani mathur:
    hello...........

    as Kalyan Anand said i must put mysql.jar file



    The same i have told earlier. I guess you have missed it !


    ...and not SQl Server authentication



    Are you using MySQL or SQL Server?

    Please be patient when reading the replies as well acknowledging. It would save and avoid further confusions
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi all...!!!

    Ooopss sorry sorry i mistakened again
    i am using SQL server and not MySql
    so i think the driver i am using is correct.

    hey raghavan

    Please read about the MySQL Connector J and its description here.



    i have seen this page but still not clear with the idea
    of using MySql Connector J.

    and Elanges waran i tried to download mysql.jar
    i got mysql.zip i unzipped it
    but i couldnt figure out actualy it has three folders
    com,org Meta-INF
    which one i must link to my project?

    please specify that

    and then i will change the driver as well
    you have mentioned to use
    Class.forName ("com.mysql.jdbc.Driver");


    lets see i could get the output as expectated.

    Thanks to all for all the help till now.

    Dhwani:>its always too soon to quit.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Originally posted by dhwani mathur:

    Ooopss sorry sorry i mistakened again
    i am using SQL server and not MySql
    so i think the driver i am using is correct.




    and Elanges waran i tried to download mysql.jar
    i got mysql.zip i unzipped it
    but i couldnt figure out actualy it has three folders
    com,org Meta-INF
    which one i must link to my project?

    please specify that

    and then i will change the driver as well
    you have mentioned to use
    Class.forName ("com.mysql.jdbc.Driver");




    Hello Dhwani,

    Please hold on for a moment. Be patient first. Be Clear on what you do.

    See the above two sections. First you told, you are using SQL Server and in the next, you say you try to make use of mysql's jar file! How it will be comptaible?

    Just think over. You are not supposed to use MySQL's jdbc driver when you use SQLServer. They both are of totally different vendors. You should look for SQL Server's JDBC driver now. Are you clear now?

    Try your luck now with SQL Server's JDBC Driver! Good luck
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Heloooo Raghavan!!

    i am again in wrong direction sorry...



    i realy need to have some patience
    while i work i dont know how
    i mixed up with two different concepts
    of SQL Server and MySql.


    yes i must try on SQL Servers JDBC driver

    now i am clear with what i must use and what not......
    and will come back as i get some solution on it.

    it feels like



    but still dont want to give up with this program.


    Dhwani:>Its always too soon to quit.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    That is okay and alright dhwani.

    I guess you need a *break*. Just take a break and start afresh with the clear mindset without confusing yourself much!
     
    Baseet Ahmed
    Ranch Hand
    Posts: 225
    Spring Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    [Hi-jack removed]

    Baseet Ahmed,
    Please start your own thread if you have a similar question.
    Adding your issue to someone else's thread is called hi-jacking and is considered rude.
    [ January 18, 2008: Message edited by: Ben Souther ]
     
    Baseet Ahmed
    Ranch Hand
    Posts: 225
    Spring Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Ok.
    If it is called hi-jacking,
    then it has to be banned for all users and in all cases.

    Will post a fresh thread on the same topic.


    Thank you.

    Regards
    Baseet Ahmed.

    "God has made every living thing from water."
     
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Hi Dhwani,

    I see the last post was on Jan 17th, I assume your problem is solved.
    If you are still having a problem you can use JTDS driver to connect to SQLServer.

    Thanks,

    Ankur
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi ankur

    my problem is not yet solved
    i was just studying so that may be i could find some solution to it.
    Recently while studying i came to know about
    <Realm > in the server.xml file but
    still i am not clear with it......
    you have suggested on JTDS can you
    ellaborate on it........

    Dhwani:>Its always too soon to quit.
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Conversation has moved to:
    http://www.javaranch.com

    dhwani,
    Please try to keep this issue in one thread.
    If you start to think that it would do better in another forum, just ask a moderator to move it for you.

    -Ben
     
    Are you okay? You look a little big. Maybe this tiny ad will help:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
      Bookmark Topic Watch Topic
    • New Topic