Results 1 to 7 of 7
  1. #1
    nancyv is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default problem in connecting to mysql database

    Hi all
    I tried to run the following code..to connect to the database but getting Exception: com.mysql.jdbc.Driver....

    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;

    public class dataservlet extends HttpServlet {



    public void doGet (HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    int emid=0;
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/html");

    String url = "jdbc:mysql://localhost/shipment";

    String query = "SELECT user_id FROM tbl_user";


    try {

    Class.forName("com.mysql.jdbc.Driver").newInstance ();


    Connection con = DriverManager.getConnection(url,"nancy","");

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery (query);

    while(rs.next())
    {
    emid=rs.getInt("user_id");
    }
    out.println(emid);

    rs.close();
    stmt.close();
    con.close();

    }

    catch (SQLException ex) {

    out.println ("SQL Exception: " + ex.getMessage());


    }

    catch (java.lang.Exception ex) {


    resp.setContentType("text/html");
    out.println ("Exception: " + ex.getMessage ());
    }

    }


    }

    I cannot connect to the database at all..I have created the database and tables correctly...Please help in finding my mistake...Thank you

  2. #2
    cheeryTomato is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    Maybe you can try to change the class driver for this one and see whether it can solve the problem:
    Class.forName("org.gjt.mm.mysql.Driver");

    If still doesn't work, you can try to set the jdbc using the ip address instead of localhost and see whether it works or not.

  3. #3
    Join Date
    Mar 2008
    Posts
    13
    Rep Power
    0

    Default

    where u r doing the program actually from console or Eclipse editor
    If its from console may be the calss path connection
    u just verify classpath is set corectly or not to u realted jar file

  4. #4
    Join Date
    Mar 2008
    Posts
    13
    Rep Power
    0

    Default connection from servlet to database

    Me also getting the same problem
    Did u get the solution how u relove u r problem
    Please tell me how did u solve that

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    10

    Default

    Have you checked the sqljdbc.jar (JDBC driver) is set correctly in the classpath??
    Did this post help you? Please me! :cool:

  6. #6
    Join Date
    Mar 2008
    Posts
    13
    Rep Power
    0

    Default I sovel my problem

    thnQ for the reply

    I solved my problem some one suggets add the ojdbc14.jar file into web-inf\lib directory then i got the ans correctly

  7. #7
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    10

    Default

    Brilliant! Nice one.

    Can you please now mark this thread as Solved. Thank you.
    Did this post help you? Please me! :cool:

Similar Threads

  1. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 08:39 AM
  2. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 02:28 PM
  3. Replies: 0
    Last Post: 03-29-2008, 04:36 PM
  4. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 AM
  5. connecting to mysql database
    By javagal in forum NetBeans
    Replies: 2
    Last Post: 08-04-2007, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •