×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Location
    Plymouth
    Posts
    27

    JDBC & MySQL problems

    Hi guys,
    I am a newbie in Java & SQL. I am currently working on a WAP project using JSP & MySQL as the DBMS. My questions:

    1. Java compiler: I saw in the manual we can use javac -sourcepath <dir> <file>. But when i tried to do that, it generates errors that says "Error: cannot read: filename.java". It works only when i place the .java file in the bin directory of the JDK and compile it with javac filename.java, it works! What is the problem? It is a real hassle everytime i need to copy those servlets to JDK bin's directory, compile and copy back to my development directory. For your information my JDK dir is in d:/fyp/JDK1.3.1 and my Tomcat development dir is in d:/fyp/Tomcat/webapps/Root/WEB-INF/classes. Can someone also explain what's the difference wbetween javac -sourcepath, javac -classpath and javac -d <dir>?

    2. JDBC.
    I am using mysql jdbc driver called 'org.gjt.mm.mysql.Driver' where it's .jar file was placed in the 'lib' directory in Tomcat. Please tell me if im correct. What is the easiest way to test that my JDBC driver works? How can I test it with a database that I have created in 'd:/mysql/data/projectdb' with the JDBC driver in Tomcat since BOTH of them are in different directories (JDBC in d:/fyp/Tomcat & MYSQL is in d:/mysql)?

    3. Web.xml
    do i need to tweak the VALUES in the lines in web.xml to make sure that my JDBC driver works?
    ........
    <param-name>dbdriver</param-name>
    <param-value>org.gjt.mm.mysql.Driver</param-value>
    ........
    <param-name>database</param-name>
    <param-value>jdbc:mysql://localhost/projectdb</param-value>
    ........
    <param-name>dbuser</param-name>
    <param-value>user</param-value>
    ........
    <param-name>dbuserpwd</param-name>
    <param-value>123456</param-value>
    ........

    Please advice how can i make this -> jdbc:mysql://localhost/projectdb possible since Tomcat & MySQL is both in different directories?


    Please help. I'm stuck.
    Regards,
    cyw

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    RE: JDBC & MySQL problems

    Hi,

    1. '-sourcepath', '-classpath' and '-d' are all explained in the help of javac (javac -help). Of course knowing what they are is not the same as knowing how to use them. I wish all -help included simple examples. I had to learn it the hard way, so here is an example for you:

    javac -classpath "path_to_tomcat\common\lib\servlet.jar" -sourcepath "path\to\servlets\src\" -d "path_to_tomcat\webapps\yourapp\WEB-INF\classes" *.java

    Getting the syntax right is important... i.e. '\' for windows or '/' for linux. The double quotes proved necessary in my case, and even the trailing '\' in the sourcepath (wouldn't work without it).

    2. Isn't that driver written by the guy now working for MySQL? If so then it's been discontinued. You should use the renamed Connector/J (http://www.mysql.com/products/connector-j/index.html). All the instructions are in the distribution zip. The jar should be in the common/lib indeed (along with all the other jars).

    3. I never needed to change or add anything to web.xml. In my case all the info needed is hard-coded in the source (nobody's perfect). Example:

    java.sql.Connection conn;
    String myQuery = "SELECT * FROM mytable;";

    try
    {
    Class.forName("com.mysql.jdbc.Driver&quot.newInstance();

    conn = DriverManager.getConnection("jdbc:mysql://localhost/myDB?user=johnsmith&password=johnspass"

    Statement st = this.conn.createStatement();
    ResultSet rs = st.executeQuery(myQuery);

    // rs should now include your results
    // do whatever you want with them, like loop and print
    while(rs.next())
    {
    System.out.print("["+rs.getString("column1&quot+"] "
    System.out.println("["+rs.getString("column2&quot+"]"
    // etc...
    }
    }
    catch (Exception e) { System.err.println(e); }


    Hope this helps...

    Regards

    Dan

Posting Permissions

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