Connecting java with mysql on mac os 10.5

  1. Install MySQL downloaded from here (choose one appropriate with your mac os 10.5)
  2. Install JDBC Connecter downloaded from here. Extract the file and find “mysql-connector-java-5.1-bin.jar”. Copy the file to “library/java/extension”
  3. Compile this code to make sure that the connection works well
import java.sql.*;
public class connect{
   public static void main(String argv[]) throws Exception {

   Class.forName("com.mysql.jdbc.Driver");

   Connection conn = DriverManager.getConnection(
   "jdbc:mysql://localhost:3306/test",
   "root",
   "rahasia"
 );

 Statement stmt = conn.createStatement();
 ResultSet rset = stmt.executeQuery("SELECT now();");

 while (rset.next()) {
 System.out.println(rset.getString(1));
 }

 rset.close();
 stmt.close();
 conn.close();
 }
 }

Notes: thanks for tutorial here.

Useful links:

  1. http://www.wbluhm.com/MySQLTut.html
  2. http://www.pantz.org/software/mysql/mysqlcommands.html
  3. http://webdosen.bl.ac.id/dosen/050023/materi/koneksi_java_mysql.pdf
  4. http://www.kitebird.com/articles/jdbc.html
Advertisements