
-----------------------------------
jskopek
Tue Apr 26, 2005 11:31 am

MySQL and Java
-----------------------------------
Hey everyone. Been lurking around these forums for a while and finally decided to register. I'm working on a small program that should be relatively easy to create, but I'm having some problems getting started. The project in question is a java application that will connect to an exisitng mysql database and list the values of a certain talbe. It's essentially the simplest kind of databsae connection program you can make, but I'm having a few problems.
From the research I've done it seems like the following steps need to be taken to communicate with a database
1) A JDBC driver is loaded
2) A connection is made with the database
3) Queries are exectued
It seems simple enough (at least when the java documentation explains it:  http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html), but I haven't found any examples that communicate with MySQL. From my understanding, all that needs to be changed is the JDBC driver, but I have no clue what to load, where to begin, etc.
In short, I'm a little lost and very confused. Have any of you created a java application that communicates with a MySQL database? I promise that if one of you guys help me get an application working I'll post a tutorial on it in the tutorials section
Thanks :)

-----------------------------------
Hikaru79
Tue Apr 26, 2005 11:56 pm


-----------------------------------
Hehe... I'm working on the exact same thing ( a project involving a MySQL backend). First of all, You need to have at least Java 1.2 for JDBC to be in there. I'll assume you do. Then you need to get the JDBC driver for mySQL. If you're on Linux, lucky for you this driver is available on most distributions. On gentoo, I just did 'emerge jdbc-mysql'. For Windows you'll have to get the library yourself and add it to your classpath. It can be downloaded from: http://dev.mysql.com/doc/mysql/en/java-connector.html

If you're using an IDE such as Netbeans, you can use the Library Manager to get it recognized, too. Get this far, make sure that your Java can see the driver, and then reply here so I can go over the next step with you :)

-----------------------------------
JackTruong
Tue May 03, 2005 8:53 pm


-----------------------------------
/**
 * Make sure you download mmMySQL from the following site:
 * http://www.mysql.com/downloads/api-jdbc.html
 * Install it into the \lib\ext
 * Only put the mysql-connector-java-3.1.8-bin.jar
 *
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

public class MySQL
{
    static Connection connection = null;
    public static void startConnection () throws Exception
    {
        try
        {
            // Load the JDBC driver
            String driverName = "com.mysql.jdbc.Driver";
            Class.forName (driverName);
            // Create a connection to the database
            String serverName = "xxx.xxx.xxx.xxx"; // The address to the server.
            String mydatabase = "String"; // The database name
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
            String username = "String";
            String password = "String";
            // The username and password to the MySQL server, make sure it has access.
            connection = DriverManager.getConnection (url, username, password);
        }
        catch (SQLException e)
        {
            System.out.println ("Could not connect to the database");
        }
        finally
        {
            System.out.println ("Connected.");
        }
    }


    public static void query () throws Exception
    {
        Statement stmt = null;
        ResultSet rs = null;
        String theQuery = "SELECT * FROM ibf_members order by id";
        try
        {
            stmt = connection.createStatement ();
            stmt.setMaxRows (20);
            rs = stmt.executeQuery (theQuery);

            if (stmt.execute (theQuery))
            {
                rs = stmt.getResultSet ();
            }
            ResultSetMetaData rsmd = rs.getMetaData ();
            int numColumns = rsmd.getColumnCount ();
            System.out.println (numColumns);
            while (rs.next ())
            {
                for (int i = 1 ; i 