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

Thread: Connection to MySql Database

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Connection to MySql Database

    Hi,

    I'm just trying to get a simple connection going to my database. The database is fine but when I run my java program, all I see is, "Build Successful". I would like to display the information I put in my table. Thanks.

     
     
    import java.sql.*;
     
    public class TestSQL {
     
        public static void main(String args[]) {
            try {
     
                // Load the JDBC driver
                Class.forName("src.com.mysql.jdbc.Driver");
                System.out.println("---Driver loaded---");
     
                //Establish Connection
                Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost/test", "username", "password");
     
                //Create a Statement
                Statement statement = connection.createStatement();
     
                //Execute a Statement
                ResultSet rs = statement.executeQuery("SELECT * FROM people");
     
                //Iterate through the results and display data
                while(rs.next()){
     
                    System.out.println(rs.getString(1) + rs.getString(2));
                }
     
     
                //Close connection
                connection.close();
     
     
     
            } catch (Exception e) {
     
                e.getMessage();
            }
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Connection to MySql Database

    all I see is, "Build Successful"
    Did you execute the main class file? I only ask since this statement makes me think you compiled but didn't run the program. If you did run and didn't see anything, use println to debug, and especially when catching exceptions print something out that might be meaningful (such as e.printStackTrace()). If you do get an exception and don't understand please post back the full error message.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Connection to MySql Database

    Im using Netbeans so Im compiling and then running.
    I took your suggestion to printStackTrace and i get the following error.
    java.lang.ClassNotFoundException: src.com.mysql.jdbc.Driver
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:169)
    	at TestSQL.main(TestSQL.java:13)

    Just to let you know as well, I've already downloaded the Connection Driver and placed the folder in "/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Connection to MySql Database

    ClassNotFoundException: src.com.mysql.jdbc.Driver
    The JVM can not find the class shown in the error message.
    Are you sure that is the correct name for the class?
    Is the class file on the classpath where the JVM can find it?

    Sorry I don't know anything about using Netbeans.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Connection to MySql Database

    I think i have to change my class path, but Im not sure how to do this on mac. Ive been reading my text book and it suggests, "To use the MySQL and Oracle drivers, you have to add mysqljdbc.jar and ojdbc6.jar in the classpath using the following DOS command on Windows:
    set classpath=%classpath%;c:\book\mysqljdbc.jar;c:\boo k\ojdbc6.jar" ...If anyone knows what the equivalent command for unix is, I think that should solve the problem

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Connection to MySql Database

    Hi All,

    I finally figured out the problem, or at least one possible solution. In Netbeans you have to supply the path to the connection driver (JAR) from the projects properties.sql.jpg

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Connection to MySql Database

    For what its worth, I'd recommend not placing the driver .jar in the JDK lib folder...imagine updating your jdk and poof...that lib folder gets wiped and all your projects break as a result.

  8. The Following User Says Thank You to copeg For This Useful Post:

    dego89 (May 25th, 2011)

  9. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Connection to MySql Database

    Thanks, I realize this is a temporary fix. If I find a better suited solution I will post back.

  10. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Connection to MySql Database

    My final solution was to keep the driver (JAR) in the project folder. many thanks again.

Similar Threads

  1. Datsource connection with mysql, need help
    By zidan007 in forum JDBC & Databases
    Replies: 1
    Last Post: December 7th, 2010, 02:24 AM
  2. MySQL Database
    By pjeremy90 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 12th, 2010, 10:34 AM
  3. [SOLVED] Get Data From Database MySQL using ComboBox
    By Simple in forum Java Theory & Questions
    Replies: 2
    Last Post: June 4th, 2010, 02:45 AM
  4. Download File from Mysql database
    By Puk284 in forum Java Servlet
    Replies: 0
    Last Post: April 24th, 2010, 07:43 AM
  5. problems with connection to mysql database
    By thepower in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2010, 02:59 AM