Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: jdbc connection problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jdbc connection problem

    i don't know what is the problem with my code,
    can pls someone help me on this one, pls.

    hire's my code:
    import java.sql.*;
     
    public class Connect {
    	public static void main(String[] args) {
    		Connection conn = null;
     
    		try {
    			String userName = "root";
    			String password = "kickass";
    			String url = "jdbc:mysql://localhost:3306/student";
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			conn = DriverManager.getConnection(url, userName, password);
    			System.out.println("Database connection established");
    		} catch (Exception e) {
    			System.err.println("Cannot connect to database server: " + e.getMessage());
    		} finally {
    			if (conn != null) {
    				try {
    					conn.close();
    					System.out.println("Database connection terminated");
    				} catch (Exception e) { /* ignore close errors */
    				}
    			}
    		}
    	}
    }

    and hire's the error:
    Cannot connect to database server: com.mysql.jdbc.Driver


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: jdbc connection problem

    A simple guess is that the database is not running or there is a firewall blocking the connection.

    But just to rule out other issues, change your code to this, run it and paste the output in this thread again.

    import java.sql.*;
     
    public class Connect {
    	public static void main(String[] args) {
    		Connection conn = null;
     
    		try {
    			String userName = "root";
    			String password = "kickass";
    			String url = "jdbc:mysql://localhost:3306/student";
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			conn = DriverManager.getConnection(url, userName, password);
    			System.out.println("Database connection established");
    		} catch (Exception e) {
    			System.err.println("Cannot connect to database server: " + e.getMessage());
                            [b]e.printStackTrace();[/b]
    		} finally {
    			if (conn != null) {
    				try {
    					conn.close();
    					System.out.println("Database connection terminated");
    				} catch (Exception e) { /* ignore close errors */
    				}
    			}
    		}
    	}
    }

    // Json
    Last edited by Json; June 11th, 2010 at 07:36 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: jdbc connection problem

    Another quess is that the Class.forName() is not finding the class.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Json (June 14th, 2010)

  6. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: jdbc connection problem

    Thats a more likely one indeed.

    // Json

  7. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jdbc connection problem

    sorry for late reply, hire is the error.
    i don't get this, please help me on this one. tnx
    Cannot connect to database server: com.mysql.jdbc.Driver
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:169)
    	at test.Connect.main(Connect.java:13)

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: jdbc connection problem

    ClassNotFoundException: com.mysql.jdbc.Driver
    The JVM is not finding the class named in the error message. You need to find the jar file containing that class and add it to the classpath for the JVM, so it can find it.

  9. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jdbc connection problem

    can you give a step by step procedure, if its ok, im using eclipse IDE, tnx.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: jdbc connection problem

    Sorry I don't use an IDE.

  11. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jdbc connection problem

    I had the same problem.
    You need to download Connector/J ( MySQL :: Download Connector/J ).
    Now add the path of the mysql-connector-java-3.1.14-bin.jar(I downloaded this one) file to CLASSPATH.
    set CLASSPATH=%CLASSPATH%;c:\<file location in >\mysql-connector-java-3.1.14-bin.jar;

  12. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: jdbc connection problem

    Just download the connector like described above, put the connector jar file in your applications lib folder, in eclipse right click your project, click properties.

    Click "Java Build Path" on the left.
    Go to the "Libraries" tab.
    Click "Add Jars"
    Browse to the projects lib folder and add the connector jar file
    Close all dialogs and try running the program in Eclipse like normal.

    // Json

  13. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jdbc connection problem

    use this
    driver to connect and use connectorJ Driver and keep the jar file in the class path
    this solves ur problem
    org.git.mm.mysql.driver

Similar Threads

  1. Help with JDBC
    By michaelmel in forum JDBC & Databases
    Replies: 0
    Last Post: May 14th, 2010, 07:11 AM
  2. jdbc connection with phpmyadmin
    By anand_fevi in forum JDBC & Databases
    Replies: 1
    Last Post: May 13th, 2010, 02:26 AM
  3. Problem With JDBC
    By il912 in forum JDBC & Databases
    Replies: 7
    Last Post: October 17th, 2009, 03:59 AM
  4. JNDI - JDBC Integration problem
    By rangarajank in forum JDBC & Databases
    Replies: 0
    Last Post: September 29th, 2009, 06:07 AM
  5. JDBC Problem
    By seejay in forum JDBC & Databases
    Replies: 1
    Last Post: September 16th, 2009, 07:55 AM