Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JNich

macrumors newbie
Original poster
May 14, 2007
2
0
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!
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, 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 - http://java.sun.com/docs/books/tutorial/jdbc/ - 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.
 
Download mysql from here:
http://dev.mysql.com/downloads/mysql/5.0.html#macosx-dmg
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 Terminal.app and connect to the database. I have put the line
PATH="/usr/local/mysql/bin:${PATH}"
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 dev.mysql.com has a good reference http://dev.mysql.com/doc/refman/5.0/en/index.html 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
http://dev.mysql.com/downloads/connector/j/5.0.html
and move the .jar file 'somewhere permanent' (I put stuff I use to develop with in ~/Library/Java/JDBC)

Download eclipse (or netbeans)
http://www.eclipse.org/downloads/
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 Example.java
Code:
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
    {
        Class.forName("com.mysql.jdbc.Driver");
        
        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 (results.next())
        {
            int    digit = results.getInt(1);
            String word  = results.getString(1);
            
            System.out.println("Digit : " + digit + " Word : " + word);
        }
        System.out.println("Done");
        
        results.close();
        statement.close();
        mySQLConnection.close();
    }
}
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!)
 

kiang

macrumors regular
Apr 8, 2007
129
0
@ 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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.