Monday, October 30, 2006

MySQL over the lan

Setting up an externally viewable mysql database server is relatively easy. I used Solaris 10/sparc64 as the mysql server and WindowsXP/jdk1.5.0 as the client. It's running something ancient, like:

Last login: Wed Oct 25 18:51:39 2006 from 192.168.1.104
Sun Microsystems Inc. SunOS 5.10 s10_72 December 2004
$ mysql --version
mysql Ver 12.21 Distrib 4.0.15, for sun-solaris2.10 (sparc)
$


This is something of a milestone for me, as even during Software Engineering class I had the mysql server local on the windows box. Suffice to say here's the best way to do this: read the documentation. Mysql has great stuff on their website as does Java on their site. I could tell you the steps I used but that's really not necessary, read the documentation and take your time. (If you would indeed like me to i could write up something of a tutorial.) The one thing i'm not entirely sure of is how secure this data flow is, but it probably doesn't matter as long as it stays on the 'local' area network! I'll probably load up Ethereal sometime to see.

Here's the java code i wrote to test the connection:

/*
* Main.java
*
* Created on October 23, 2006, 10:19 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

/**
*
* @author James
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance ();
String url = "jdbc:mysql://192.168.1.106:3306/";
Connection conn = DriverManager.getConnection(url, "userNameHere", "userPasswordHere");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT Host, User FROM mysql.user;");
while (rs.next()) {
System.out.println(rs.getString("Host") + " " + rs.getString("User"));
}

} catch (Exception ex) {
// handle the error
System.out.println("sup now bitch" + ex.getMessage());
}
}

}


Sorry about the tabs getting chopped off.