Help with Java SQL programming on Xcode

Discussion in 'Mac Programming' started by JNich, May 14, 2007.

  1. JNich macrumors newbie

    May 14, 2007
    I recently bought (last fall) an Ibook with Xcode, and have a question about using the java.sql package. I don't have microsoft access, and I don't think I have a native database program on my computer, so, can anybody give me some help on getting started (downloading the right software, setup etc) so I can use this library. I have some experience in java and sql programming, but it's has been a few years. Thanks!
  2. pilotError macrumors 68020


    Apr 12, 2006
    Long Island
    mysql is a good start. Its pretty standard on a bunch of platforms and open source.

    Get started here:

    There are a few others, filemaker, openbase, etc., I don't know much about them.

    Once you get mysql installed, you should just be able to create the connect string and go.
  3. JNich thread starter macrumors newbie

    May 14, 2007
  4. jeremy.king macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    Your database doesn't have to exist locally.

    In any case, anything that supports JDBC is a good choice. Check out the tutorials at Sun - - The only caveat is that most database vendors use their own JDBC connect strings.

    PS. XCode isn't the most ideal IDE for Java development, you may want to check out Eclipse or NetBeans if you want serious tool support.
  5. AlmostThere macrumors 6502a

    Download mysql from here:
    and install the database and the preference pane, which will let you startup and shutdown mysql from the system prefences window.

    Open System Preferences and start up MySQL. You can now start and connect to the database. I have put the line
    in my ~/.bash_profile
    If you do this, open a new Terminal and type
    mysql -u root
    you are then connected to the database running on your computer.
    You can then create a database, tables, etc. all through the command line using all the SQL you know. Lots of good tutorials on the net if you need to refresh your memory and has a good reference if you don't.
    Here go for
    create database testdb;
    use testdb;
    create table testtbl (digit integer, word varchar(10));
    insert into testtbl values (1, 'one'), (2, 'two');
    select * from testtbl;

    Download the JDBC driver from here
    and move the .jar file 'somewhere permanent' (I put stuff I use to develop with in ~/Library/Java/JDBC)

    Download eclipse (or netbeans)
    Untar it and copy into a folder in Applications

    Open up Eclipse, accept the default workspace or set it to your directory of choice, close the tutorial window (you can come back later) and then select File -> New -> Project ...
    Then Java Project (top of the list), hit next, then enter a name for your project, hit next, and on the next window, select Libraries, the Add External Jars then navigate to the jar file you downloaded. Then click Finish.

    Then select File -> New ->Class
    Call it Example (or something like that). Add package org.test or something to hide the warning message. Select the
    public static void main(String [] args)
    check box.

    Enter the code below in
    package org.test;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class Example
        public static void main(String[] args) throws Exception
            String url  = "jdbc:mysql://localhost:3306/testdb";
            String user = "root";
            String pass = "";
            Connection mySQLConnection = DriverManager.getConnection(url, user, pass);
            Statement statement = mySQLConnection.createStatement();
            String    query     = "SELECT * FROM testtbl";
            ResultSet results   = statement.executeQuery(query);
            System.out.println("Table Contents");
            while (
                int    digit = results.getInt(1);
                String word  = results.getString(1);
                System.out.println("Digit : " + digit + " Word : " + word);
    Then select on the top menu Run -> Run...
    If you double click on Java Application (this might happen automatically, scanning for the main(...) function) it should create a new entry under Java Application called Example.

    Then click Run to run connect to the database and read the contents of the table.

    That should be about it ... the rest will come back to you after that.

    (Just spent the afternoon getting some guys started with exactly the same thing - good timing!)
  6. kiang macrumors regular


    Apr 8, 2007
    @ the code above:
    In a real app I'd put the SQL operations in a try block and the close statements in a finally block, just to be safe and not overloading your SQL connections:p

Share This Page