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 6 of 6

Thread: connect to mysql database

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default connect to mysql database

    I'm using the following code to try to establish a connection with my database, I get "Cannot connect to database server" when I run it.

    When I debug, I get this list of error but it doesn't effect the build

    debug:
    Have no file for /usr/lib/jvm/java-6-openjdk/jre/lib/netx.jar
    Have no file for /usr/lib/jvm/java-6-openjdk/jre/lib/plugin.jar
    Have no file for /usr/lib/jvm/java-6-openjdk/jre/lib/modules/jdk.boot.jar
    Called the mysqlconn method!
    Cannot connect to database server
    BUILD SUCCESSFUL (total time: 0 seconds)

     
    import java.sql.*;
     
    public class Main {
     
        public static void main(String[] args) {
            mysqlconn();
        }
    public static void mysqlconn(){
            System.out.println("Called the mysqlconn method!");
            Connection conn = null;
     
               try
               {
                   String userName = "root";
                   String password = "password";
                   String url = "jdbc:mysql://localhost:3306/test";
                   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");
               }
               finally
               {
                   if (conn != null)
                   {
                       try
                       {
                           conn.close ();
                           System.out.println ("Database connection terminated");
                       }
                       catch (Exception e) { /* ignore close errors */ }
                   }
               }
    }
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: connect to mysql database

    catch (Exception e)
               {
                   System.err.println ("Cannot connect to database server");
               }

    It is always best practice to gather information from the exception - typically through something like e.printStackTrace() - just printing there is an error doesn't tell you much. Post the exception in its entirety

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connect to mysql database

    I've changed my code to this, which it doesn't seem to like all that much, it says "throwable print stack should be removed"

    catch (Exception e)
               {
                   e.printStackTrace();
               }

    I ran the code again and got this exception;

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

  4. #4
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: connect to mysql database

    Quote Originally Posted by aueddonline View Post
    [/code]

    I ran the code again and got this exception;

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    Make sure that you have mysql-connector-java-xxx.jar present in the classpath.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 03:05 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connect to mysql database

    I'm using netbeans 6.9

    tools>library

    then I created a new library called mysql and added;

    /usr/share/java/mysql-connector-java-5.1.10.jar &
    /usr/share/java/mysql-connector-java.jar

    still the same deal

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connect to mysql database

    What a mission I made that,

    In the left hand panel there is the libraries folder, I downloaded mysql-connector-java-5.1.18-bin.jar are save it to disk. The right clicked the libraries folder and select 'Add Jar/folder' and then find the mysql-connector-java-5.1.18-bin.jar and added it.

    first I tried doing a similar thing in tools>libraries but this didn't seem to work

    Anyway it's working now, what a complete pain, I just wanted to program!

Similar Threads

  1. How to connect to a remote database?
    By ace84 in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 01:09 AM
  2. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 04:45 PM
  3. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 03:09 PM
  4. connect java to a database
    By ridg18 in forum JDBC & Databases
    Replies: 1
    Last Post: December 25th, 2010, 10:28 AM
  5. Need urgent help to connect to Database
    By The Vicky in forum JDBC & Databases
    Replies: 3
    Last Post: November 27th, 2010, 10:09 AM