Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java/Mysql

Options
  • 01-07-2009 11:48pm
    #1
    Registered Users Posts: 86 ✭✭


    Can anyone recommend any good online tutorials for setting up java and mysql to work together. I need to start with how to install mysql. Any help would be great. Thanks guys.


Comments

  • Closed Accounts Posts: 448 ✭✭ve


    A quick Google for "java mysql tutorial" gives us this...

    http://www.developer.com/java/data/article.php/3417381


  • Registered Users Posts: 427 ✭✭Kevo


    Installing mysql is pretty simple. Just download the windows installer and follow the on screen instructions.

    I assume you already have the java sdk installed.

    Now, once mysql is running, all you have to do is include a JDBC driver in the build path of your java program. Just download it online.

    Then you can access the database from your java code as follows:
    Connection con = DriverManager.getConnection
               ( "jdbc:myDriver:wombat", "myLogin","myPassword");
    			  
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
    


    It's all in the tutorial.


  • Closed Accounts Posts: 7 itscout


    NerfNerder wrote: »
    Can anyone recommend any good online tutorials for setting up java and mysql to work together. I need to start with how to install mysql. Any help would be great. Thanks guys.

    You don't mention on which OS you plan to run this or if Java + MySQL are to be used in a web application or a standalone one.

    Assuming you are interested in the webapp side as well, a quick way to get up and running in development mode is to download a Glassfish (Java servlet container) + MySQL bundle from Sun
    http://java.sun.com/javaee/downloads/index.jsp
    There's even a bundle including the NetBeans IDE
    http://download.netbeans.org/netbeans/6.1/mysql_bundle/
    (or you can choose Eclipse)
    Even if you don't plan to use the webapp part, this may simplify the basic MySQL setup for you.

    As for how to connect to MySQL using Java, I think this provides a good
    learning ladder:

    1) http://java.sun.com/developer/technicalArticles/mysql_java/index.html
    This is the most basic way of using MySQL in Java, and it's useful IMO to learn this first

    2)

    a) Then you can learn about more sophisticated things like DB connection pooling. Assuming you plan on using an application server, this shows you how to quickly setup a connection pool in Glassfish and get a connection from it in Java.

    b) Another way of decoupling your code from the low level details of connecting to a database is to use Spring. See this tutorial
    This moves the details of connecting to the database in the Spring configuration file. It also allows your code to stay completely unchanged while changing how you connect to the database.

    The tutorial above shows you how to have Spring connect directly to the database (see the bean id="datasource" part). If you wanted Spring to use the connection pool from an app server (like one set up in 2) a) ) you'd just change the "datasource" bean to become something like this:

    <bean id="datasource" class="org.springframework.jndi.JndiObjectFactoryBean" >
    <property name="jndiName" value="java:comp/env/jdbc/btdConnDS"/>
    </bean>

    3) Finally, you could go one level higher and look into using an ORM tool like Hibernate. For sophisiticated enough projects this can both improve the performance of your application (via caching, batching DB operations) as well as simplify your task by generating (some of) the SQL for you.

    A Spring + Hibernate tutorial is here
    Happy hacking !
    --
    IT Scout - www.jobscout.ie


  • Registered Users Posts: 819 ✭✭✭sixpack's little hat


    I found the following tutorial really helpful when learning how to use Java and MySQL last year: http://www.developer.com/java/data/article.php/3417381

    It literally goes through every step you need to follow, it is one of the best programming tutorials I have ever used.


Advertisement