Well, Java and MySQL works just fine via ODBC in a Java app I recently wrote. I made no special configurations on the Java or MySQL side, I just installed a binary of MySQL (4.0.12), downloaded the latest Java ODBC driver from the MySQL site, and it worked. Note that the MySQL ODBC driver needs to be somewhere where the Java compiler will be able to find it. Some sample Java code:
import java.sql.*;
.
.
.
static String mysql_serverName = "your.server.name";
static String mysql_mydatabase = "your.db.name";
static String mysql_username = "your_mysql_user_name";
static String mysql_password = "your_password";
static public Connection connect_to_db()
/*
Connect to the target database
*/
{
try
{
connection = null;
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String url = "jdbc:mysql://" + mysql_serverName + "/" + mysql_mydatabase;
connection = DriverManager.getConnection(url,mysql_username,mysql_password);
}
catch (ClassNotFoundException cne)
{
System.out.println("Class not found exception in connecting to db.");
System.out.println(cne.getMessage());
cne.printStackTrace();
connection = null;
}
catch (SQLException sqle)
{
System.out.println("SQL Exception in connecting to db.");
System.out.println(sqle.getMessage());
sqle.printStackTrace();
connection = null;
}
return connection;
}
And here's how I execute a query:
public void buildProjectList(MutableList targetList)
/*
Build the list of available projects on the DSS.
*/
{
connection = connect_to_db();
//Results column strings
String project_id;
String project_name = "";
int item_idx = -1;
int default_idx = -1;
if (connection == null)
{
System.out.println("Unable to create JDB connection to db. Please contact Polar Software Support.");
}//if (connection == null)
else
{
int default_set_flag;
try
{
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from dss_user_set where active=1 order by name asc");
while (rs.next())
{
item_idx++;
project_id = rs.getString("id");
project_name = rs.getString("name");
targetList.getContents().addElement(project_name);
}//while (rs.next())
//Preselect the first project as the default target project
targetList.setSelectedIndex(0);
targetList.setFixedCellWidth(list_cellWidth);
connection.close();
}//try
catch (SQLException sqle)
{
System.out.println("SQL Exception while building project group list.");
System.out.println(sqle.getMessage());
sqle.printStackTrace();
}//try
}//if (connection == null)
}
All the above worked perfectly with no special configuration of MySQL or Java, as I said I just downloaded the MySQL -> Java ODBC .jar off the MySQL site, and used the above code.
Hope this helps!
|