Results 1 to 19 of 19
  1. #1
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default mysql classpath ubuntu problem

    i am using ubuntu and i have installed mysql in it and tring to connect to mysql through following java program but it is giving error:
    java.lang.ClassNotFoundException: con.mysql.jdbc.Driver

    Java Code:
    import java.sql.*;
    class f{
    	public static void main(String s[])throws Exception{
    	try{
    		Connection con;
    		Statement stmt;
    		ResultSet rs;
    		Class.forName("con.mysql.jdbc.Driver").newInstance();
    		con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root","ubuntu");
    		stmt=con.createStatement();
    		rs=stmt.executeQuery("select * from table1");
    		while(rs.next()){
    			System.out.println(rs.getString("id")+"\t"+rs.getString("data")+"\t"+rs.getString      ("timestamp"));
    		}
    		}catch(Exception e){
    			System.out.println(e);
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you added that jar file into your class path. Actually I'm not sure how it done in Ubuntu. Read some materials on that. The error cause the VM cannot find the package you mentioned in the required place.


  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    No matter if it is Ubuntu in windows or Linux, you should insert your mysql<version>.jar in the class path

  4. #4
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    thanks for rply can u tell me about the drivers needed and how to set classpath

    because i tried many commands for setting up classpath with driver :mysql-connector-java-5.0.6-bin.jar i think the driver is right but i am not setting my classpath write

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    i solve the problem ,i followed the following steps:

    driver required are :

    sudo aptitude install libmysql-java

    command for classpath:

    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    VanGo is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default command for classpath:

    First, let me say I'm an old guy, not in any school course or anything.
    If you are going to suggest reading, be specific please.
    That said, I have read a lot, but I'm not clear on the "export" talked about here.
    My current project and resulting question was exactly the same as the thread originator munish

    But I don't know where to put the export CLASSPATH statement.
    I've tried several spots ( .bashrc ) and a couple others that i read about.

    I'm a linux, ubuntu, java beginner, so if you have an answer it needs to take that into account.

    Thanks

  9. #9
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    export comand is basically for setting the classpath in ubuntu........
    for running mysql statements you have to write this statement in terminal


    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

    for this u have to download mysql-connector-java.jar from mysql site and before all this u should type this command in terminal for driver : sudo aptitude install libmysql-java.

  10. #10
    hasanur is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Hello,
    I am a new Bee.
    MySql connector can connects NetBeans Java file but I cannot connect from JSP file.

    I am using tomcat and my jsp file is placed in WebApps folder.
    I have set the class path in .bashrc file as bellow

    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.13-bin.jar

    and the jar file is placed in that location but the index.jsp file gives me this error

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    Here is my JSP source code

    <%@page

    import = "java.sql.*"
    import = "java.util.Properties"

    %>

    <%

    Connection conn = null;
    String url = "jdbc:mysql://localhost/java_mysql_db";
    String userName = "root";
    String password = "root";

    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection(url,userName,password) ;
    out.println("Connected<br/>");

    }
    catch(Exception e) {
    out.println(e);
    }
    %>

    some forum says to keep the connector jar file into the Webserver lib folder but I cannot find lib folder of Tomcat as well.

    Need help :'( struggling for last few days.

    thanks

  11. #11
    hasanur is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Now I have keept the connector jar file in these locations

    /var/lib
    /var/lib/tomcat6/webapps/projects
    /var/lib/tomcat6/webapps
    /var/lib/tomcat6/
    /lib
    /usr/share/mysql-common
    /usr/share/tomcat6/lib
    /usr/share/tomcat6/bin

    nothing working ... i am sure i am missing something but do not know what ... so frustrating :|

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Regarding your question, I wonder that you've properly set the classpath. Open your console and type the following command.

    java com.mysql.jdbc.Driver

    What happen?

  14. #14
    hasanur is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the response. I will make new thread next time.

    I get this error from terminal after writing that command
    "Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver"

    then what should i set as a path, i have set this in .bashrc
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

    In that directory i have another package named mysql-connector-java-5.1.6.jar, so I also tried
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.6.jar

    thanks.

  15. #15
    hasanur is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Anyway, Thanks you.

    I have solved it by reinstalling latest netbenas by downloading that from their site and remove my old one. and then include the connector jar file for the project. Now its working fine.

    your forum is really helpful.
    thanks.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by hasanur View Post
    Thanks for the response. I will make new thread next time.

    I get this error from terminal after writing that command
    "Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver"

    then what should i set as a path, i have set this in .bashrc
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

    In that directory i have another package named mysql-connector-java-5.1.6.jar, so I also tried
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.6.jar

    thanks.
    I don't think that this is happen due to the command I asked you to try out, isn't it? There is no way to happen this. Command I ask you to try-out basically explain that whether you've configured it properly.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by hasanur View Post
    Anyway, Thanks you.

    I have solved it by reinstalling latest netbenas by downloading that from their site and remove my old one. and then include the connector jar file for the project. Now its working fine.

    your forum is really helpful.
    thanks.
    So that means you've use a version which has no relevant drivers. My advice is to download the compete installation package on NetBeans unless you are not aware about the dependencies on each other (means the different languages used in as an IDE)

  18. #18
    hasanur is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I don't think that this is happen due to the command I asked you to try out, isn't it? There is no way to happen this. Command I ask you to try-out basically explain that whether you've configured it properly.
    I wrote java com.mysql.jdbc.Driver in the terminal and got about `5 lines out put and that was the first line of them.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. classpath problem
    By shwein in forum New To Java
    Replies: 4
    Last Post: 04-01-2009, 12:32 PM
  2. Problem with CLASSPATH for STRUTS
    By bhupal4all in forum Web Frameworks
    Replies: 2
    Last Post: 11-07-2008, 05:48 AM
  3. Eclipse / Mysql problem
    By jdevm in forum Eclipse
    Replies: 0
    Last Post: 10-09-2008, 01:16 AM
  4. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 05:27 PM
  5. Mysql problem
    By Nick15 in forum JDBC
    Replies: 2
    Last Post: 05-15-2007, 05:07 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
  •