Tag Archives: Apache

Of course it’s possible to use Apache Derby as database for Java apps without Eclipse:

1. Download and extract Apache Derby

http://apache.atviraskodas.com/db/derby/db-derby-10.4.2.0/db-derby-10.4.2.0-bin.zip

2. Start Apache Derby database: bin/startNetworkServer.bat

3. Create database: bin/ij.bat

1
connect 'jdbc:derby://localhost:1527/myDB;create=true;user=user1;password=user1';

4. Insert some values into database:

1
2
3
4
5
CREATE TABLE restaurants(id integer, name varchar(20), city varchar(50));
INSERT INTO restaurants VALUES (1, 'Irifunes', 'San Mateo');
INSERT INTO restaurants VALUES (2, 'Estradas', 'Daly City');
INSERT INTO restaurants VALUES (3, 'Prime Rib House', 'San Francisco');
SELECT * FROM restaurants;

5. Create new Java Class, example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
 
public class Restaurants {
  private static String url = "jdbc:derby://localhost:1527/myDB;create=true;user=user1;password=user1";
  private static String table = "restaurants";
  private static Connection con = null;
  private static Statement stmt = null;
 
  public static void main(String[] args) {
    try {
      Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
      con = DriverManager.getConnection(url);
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from " + table);
      ResultSetMetaData rsmd = rs.getMetaData();
      int cols = rsmd.getColumnCount();
      for (int i=1; i<=cols; i++) {
      System.out.print(rsmd.getColumnLabel(i) + "\t\t");
    }
    System.out.println("\n----------------------------------------");
    while (rs.next()) {
      System.out.println(rs.getInt("ID") + "\t\t" +  rs.getString("NAME") + "\t\t" + rs.getString("CITY"));
    }
    rs.close();
    stmt.close();
    con.close();
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
    }
  }
}

6. Compile Java Class:
javac.exe -classpath .;derby.jar;derbyclient.jar;derbynet.jar;derbytools.jar Restaurants.java

7. Run Java Class:
java.exe -classpath .;derby.jar;derbyclient.jar;derbynet.jar;derbytools.jar Restaurants
ID NAME CITY
—————————————-
1 Irifunes San Mateo
2 Estradas Daly City
3 Prime Rib House San Francisco

That’s it.

It’s also possible with Derby to share a database. After creation and inserting data just copy the file bin/myDB (myDB is the name of the database) to another Derby bin directory – done.

Tagged with , , , .

This is how to use Apache Derby as database in Eclipse. Partly based on the Derby documentation from http://db.apache.org/derby/

1. Download Apache Derby Eclipse plugin:

http://ftp.uni-erlangen.de/pub/mirrors/apache/db/derby/db-derby-10.4.2.0/derby_core_plugin_10.4.2.zip

http://ftp.uni-erlangen.de/pub/mirrors/apache/db/derby/db-derby-10.4.2.0/derby_ui_plugin_1.1.2.zip

2. Unzip the two plugins into Eclipse’s plugin directory

3. (Re-)Start Eclipse

4. Create new Java project in Eclipse

5. Integrate Derby libs in Java project: Right-click on Project, select Apache Derby / Add Apache Derby nature

6. Start Derby server: Right-click on Project, select Apache Derby / Start Derby Network Server
The green symbol on the Java Project will indicate a running Derby server for this project.

7. Create database: Right-clickon Project, select Apache Derby / ij (interactive SQL) and enter:

1
connect 'jdbc:derby://localhost:1527/myDB;create=true;user=user1;password=user1';

8. Insert some values into database:

1
2
3
4
5
create table restaurants(id integer, name varchar(20), city varchar(50));
insert into restaurants values (1, 'Irifunes', 'San Mateo');
insert into restaurants values (2, 'Estradas', 'Daly City');
insert into restaurants values (3, 'Prime Rib House', 'San Francisco');
select * from restaurants;

9. Create new Java Class in Java project, example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
 
 
public class Restaurants {
	private static String url = "jdbc:derby://localhost:1527/myDB;create=true;user=user1;password=user1";
	private static String table = "restaurants";
	private static Connection con = null;
	private static Statement stmt = null;
 
	public static void main(String[] args) {
		createConnection();
		insertRestaurants(4, "LaVals", "Berkeley");
		selectRestaurants();
		closeConnection();
	}
 
	private static void createConnection() {
		try {
			Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
			con = DriverManager.getConnection(url);
		} catch (Exception e) {
			System.err.println("Unable to connect to database: " + e.getMessage());
		}
	}
 
	private static void insertRestaurants(int id, String name, String city) {
		try {
			stmt = con.createStatement();
			stmt.execute("insert into " + table + " values (" + id + ",'" + name + "','" + city + "')");
			stmt.close();
		} catch (SQLException e) {
			System.err.println("Error with SQL insert: " + e.getMessage());
		}
	}
 
	private static void selectRestaurants() {
		try {
			stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + table);
			ResultSetMetaData rsmd = rs.getMetaData();
			int cols = rsmd.getColumnCount();
			for (int i=1; i<=cols; i++) {
				System.out.print(rsmd.getColumnLabel(i) + "\t\t");
			}
			System.out.println("----------------------------------------");
			while (rs.next()) {
				System.out.println(rs.getInt("ID") + "\t\t" +  rs.getString("NAME") + "\t\t" + rs.getString("CITY"));
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			System.err.println("SQL error: " + e.getMessage());
		}
	} 
 
	private static void closeConnection() {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			System.err.println("Error while closing connection: " + e.getMessage());
		}
	}
}

That’s it :)

Tagged with , , , , , .

Redmine is a very interesting free project management web application based on Ruby on Rails. Unfortunately it’s not that easy to install the components necessary to get Redmine up and running on a Debian server. Therefore I describe what I’ve done here.

First we will install the Ruby packages from Debian:

1
apt-get install ruby rake rubygems libmysql-ruby librmagick-ruby

It’s possible to have an own user account for Redmine. This user needs to have the GEM_PATH set. In addition to that we prepare some directories and install rails with Gem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
adduser --home /opt/redmine --disabled-login redmine
su - redmine
redmine:~$ echo export "GEM_PATH=$HOME/gems" > ~/.bashrc
redmine:~$ mkdir gems
redmine:~$ gem install -i $GEM_PATH rails -y
redmine:~$ gem install -i $GEM_PATH rails -y
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed rails-2.2.2
Successfully installed rake-0.8.3
Successfully installed activesupport-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activeresource-2.2.2
Installing ri documentation for rake-0.8.3...
Installing ri documentation for activesupport-2.2.2...
Installing ri documentation for activerecord-2.2.2...
Installing ri documentation for actionpack-2.2.2...
Installing ri documentation for actionmailer-2.2.2...
Installing ri documentation for activeresource-2.2.2...
Installing RDoc documentation for rake-0.8.3...
Installing RDoc documentation for activesupport-2.2.2...
Installing RDoc documentation for activerecord-2.2.2...
Installing RDoc documentation for actionpack-2.2.2...
Installing RDoc documentation for actionmailer-2.2.2...
Installing RDoc documentation for activeresource-2.2.2...
 
gem update --system
rake db:migrate RAILS_ENV="production"
rake redmine:load_default_data RAILS_ENV="production"

Now let’s get Redmine:

1
2
3
4
5
wget http://rubyforge.org/frs/download.php/49319/redmine-0.8.0.tar.gz
tar xvfz redmine-0.8.0.tar.gz 
mv redmine-0.8.0 redmine 
chown -R redmine:nogroup files log tmp
chmod -R 755 files log tmp

Of course Redmine needs a database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql -u root -p
mysql> create database redmine;
mysql> grant all privileges on redmine.* to someone@localhost \
   identified by 'password'
mysql> exit
 
/opt/redmine/redmine# cp config/database.yml.example config/database.yml     
 
$editor config/database.yml
production:
   adapter: mysql
   database: tracks
   host: localhost
   username: someone
   password: password
   socket: /var/run/mysqld/mysqld.sock

That’s all for the basic Redmine installation, let’s test it!

1
/opt/redmine/redmine#  ruby script/server -e production

We should now be able to open Redmine on our server using http and port 3000 (eg http://myserver:3000).
The default Administrator login is admin/admin (change it immediately!).

Most people don’t want to start Redmine using the ruby command so we integrate Ruby and Redmine into Apache using another great product: Passenger:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
gem install passenger
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed fastthread-1.0.1
Successfully installed rack-0.9.1
Successfully installed passenger-2.0.6
3 gems installed
Installing ri documentation for fastthread-1.0.1...
Installing ri documentation for rack-0.9.1...
Installing ri documentation for passenger-2.0.6...
Installing RDoc documentation for fastthread-1.0.1...
Installing RDoc documentation for rack-0.9.1...
Installing RDoc documentation for passenger-2.0.6...
 
passenger-install-apache2-module 
Welcome to the Phusion Passenger Apache 2 module installer, v2.0.6.
 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.
 ...
Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

The Passenger installation suggests some configuration and that’s excactly what we do:

1
2
3
4
5
6
7
8
9
10
/etc/apache2/mods-available# cat passenger.load 
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
/etc/apache2/mods-available# cat passenger.conf 
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /usr/bin/ruby1.8
 
a2enmod passenger
Module passenger installed; run /etc/init.d/apache2 force-reload to enable.
/etc/init.d/apache2 force-reload
Forcing reload of web server (apache2)... waiting .

That should be it – now we can access Redmine on Ruby through Apache. You might want to use some VirtualHost configuration for it or something else – that’s Apache configuration.

Feel free to comment :)

Tagged with , , , , , , , , .