CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2006
    Posts
    144

    Why code can compiled successfully without mysql-connector-java-#-bin.jar installed

    Hello All,

    In my program, i want to use a Java servlet to response to users' web request, in which the info submitted by users is write into a remote MySQL database. To this end, I have to establish a connection to the MySQL database firsts. I do this using the following code and can compile it sucessfully. It is strange to me that the code can be compiled successfully,though there is actually not mysql-connector-java-###-bin.jar package installed/copied on the Linux box.


    Code:
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    public class welcomeyou extends HttpServlet
    {
        public void doGet(HttpServletRequest rqu, HttpServletResponse resp)
        {
             String driver="com.mysql.jdbc.Driver";
             String url="jdbc:mysql://192.168.1.1/DatabaseName";
             String databaseuser="root";
             String password=""
             Connection con = null;Statement smt =null;
             Resultset rs=null; 
             try
              {
    
                     con=DriverManager.getConnection(url, databaseuser, password);
                     smt =con.createStatement();
              }
             catch(SQLException E)
              {
                  System.out.println("SQLException:" + E.getMessage());
                 System.exit(0);
               }
       }
    }
    I cannot provide error message, since the statement System.out.println("....) could not display error message on the screen in this circumstance.(Could anybody tell me how to do this )

    I wonder if the statement 'import java.sql.*;' means import library from mysql-connector-java-###-bin.jar

    Please help me, this problem has stuck me for a long time.

    thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    70

    Re: Why code can compiled successfully without mysql-connector-java-#-bin.jar installed

    There are two ways to load a jdbc driver. Either with early binding or late binding.

    e.g:

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

    The first instance takes a string as a parameter and will look for that class at runtime and load the driver.

    The second will require the driver to be present at compile time.

    Most of the time when working with databases people tend to use late binding. As they can change a knob to alter the driver they are going to use.

    In your particular case you aren't loading any driver. Add the "Class.forName("com.mysql.jdbc.Driver").newInstance ();" before your DriverManager call. Then make sure the mysql jar is on the classpath when you run.

    Hope that helps...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured