Difference between revisions of "JDBC and MySQL"

From ArchWiki
Jump to: navigation, search
(added info about mysql-jdbc package in AUR)
Line 47: Line 47:
   cp mysql-connector-java-5.0.6-bin.jar /opt/java/jre/lib/ext/
   cp mysql-connector-java-5.0.6-bin.jar /opt/java/jre/lib/ext/
JDBC [[PKGBUILD]] is currently in [[AUR]] repository. Use [[yaourt]] to install it:
yaourt -Sy mysql-jdbc

Revision as of 18:25, 21 April 2009


This document describes how to set up your Arch system so MySQL Databases can be accessed via Java programs.


Installing MySQL

To start with, switch to the system user, then install MySQL.

su (enter your system password)
pacman -S mysql

There are now two "fixes" you have to make. Firstly edit the file my.conf to allow network access

gedit /etc/my.cnf

Find the line with skip_networking in and comment it out so it looks like this


Close that file, and edit the /etc/hosts.allow file

gedit /etc/hosts.allow

and enter on a blank line

mysqld : ALL : ACCEPT

Finally start mysql up with :

/etc/rc.d/mysqld start

Installing JDBC

JDBC is not currently in the repositories. I downloaded the current version, at the time of writing 5.06 from the MySQL website at http://www.mysql.com/products/connector-j/

Next, unpack it into a temporary directory

cd /tmp
tar xzvf /aux/arch/extras/mysql-connector-java-5.0.6.tar.gz (the exact filename will depend on where you download it to)

Finally, copy it into the java extensions directory

 cp mysql-connector-java-5.0.6-bin.jar /opt/java/jre/lib/ext/

JDBC PKGBUILD is currently in AUR repository. Use yaourt to install it:

yaourt -Sy mysql-jdbc


To access mysql's command line tool,


Creating the test database

The following commands create a database and allow a user 'paulr'. You will need to change the user name to whatever yours is.

create database emotherearth;
grant all privileges on emotherearth.* to paulr@localhost identified by "paulr";
flush privileges;

Now press Ctrl+D to exit the command line tool.

Creating the test program

Use an editor to create a file DBDemo.java with the following code in it. You will need to change the username and password appropriately.

import java.sql.*;
import java.util.Properties;
public class DBDemo
 // The JDBC Connector Class.
 private static final String dbClassName = "com.mysql.jdbc.Driver";
 // Connection string. emotherearth is the database the program
 // is connecting to. You can include user and password after this
 // by adding (say) ?user=paulr&password=paulr. Not recommended!
 private static final String CONNECTION =
 public static void main(String[] args) throws
   // Class.forName(xxx) loads the jdbc classes and
   // creates a drivermanager class factory
   // Properties for user and password. Here the user and password are both 'paulr'
   Properties p = new Properties();
   // Now try to connect
   Connection c = DriverManager.getConnection(CONNECTION,p);
   System.out.println("It works !");

Running the Program

To compile and run the program enter

javac DBDemo.java
java DBDemo

and hopefully, it should print out

This is the database connection
It works !