Connection pooling in MySQL and Java

A bit of an example. I must try and find a package for dealing with this automatically – certainly creating the connection to database is pretty slow. Sharing those connections is well worth it. Need a bit of a play sometime. I’m also wondering if connection pooling with Javascript’s XMLHTTPRequest object might be a good idea.

Advertisement
Connection pooling in MySQL and Java

Shell Execute and Java

I found that Windows’ ShellExecute (in the Win32 API) could be very useful, especially in the noddy VB6 applications I keep being asked to write.

Anyway, I was looking at doing something similar in Java.

This only works in Windows – but basically, you rely on the Windows performing it’s default action…
import java.lang.*;
public class OpenDocument {
public static void main (String [] Args ) {
System.out.println("Main:");
Process oProcess;
String cmd[] = { "cmd" , "/c", "start" , "C:\Java\OpenDocument\Hamlet.doc"};
try {
oProcess = Runtime.getRuntime().exec( cmd );
} catch ( Exception e ) {
System.out.println(e.toString());
}
}
}

In this case, the programme ‘starts’ a word document – that is, it opens up Word and loads in the document. This is done via the command prompt. Nuff said.

Shell Execute and Java

JDBC and database access

Slow day today, so I finally (after 2 years of saying I’d give it a go) got around to trying to use JDBC.

I wrote a simple class to query a mySql database. It worked like a charm! Now all I need to learn is good design practice (which could, I fear, include Beans). I’m not entirely sure how the object model matches up to the database structure – I guess that this is what I need to learn…
import java.sql.*;
import java.util.Properties;

public class JDBCTest {
public static void main( String args[] ){
Connection conn= null;
try{
String cURL = "jdbc:mysql://localhost/jtest";
Properties p = new Properties();

p.put("user","root");
p.put("password","voodoo");

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

conn = DriverManager.getConnection( cURL, p );

Statement s = conn.createStatement();
ResultSet r = s.executeQuery( "SELECT * FROM jtable WHERE iID = 1" );
r.first();
System.out.println ( r.getString("cFEN"));
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if( conn != null ) {
try { conn.close(); }
catch ( Exception e ) {}
}
}
}
}

JDBC and database access

Chinese Chess Engine Again

Work continues…

Gaze of Death – Kings may not face each other directly without an intervening piece

and I have this thing through to the point that it will read a FEN (a notation describing a board state, like
4KAr2/4A4/3Hr4/7h1/9/9/9/9/4aP1R1/3ak3C w---1
), create a board recording that, work out the valid moves for each piece (that is, the squares that that piece can move to), then work out what moves are ‘allowed’ – that is, the move doesn’t result in check, or causing the Gaze of Death.

I’ve used bit boards for most of it, but I haven’t precomputed them, as has been the case for some chess engines. The reason, mainly, was being unsure how to cope with ‘Blocking’ rules – that is, some pieces can be blocked by a piece up close to them. For example, Knights can’t jump – they move 1 square straight, and another diagonally. If there is a piece on the straight square, they can’t move that way.

I couldn’t figure out how to precompute this, though now I wonder if a second bit board with ‘blocked’ squares on it would have sufficed. Still, the precomputation might have been a bit much – 90 squares, times 8 for the number of ways it could be blocked is 720 combinations, plus 90 for the blocked squares masks.

Actually, it might be worth a look. Anyway, speed is not a huge issue for this right now – it’s only point is to determine valid moves, and I intend to cache the result in a database, so that valid moves can be referred to by FEN…

Chinese Chess Engine Again

Chinese Chess Engine

Okay, so few people in the West have heard of Chinese Chess, or Xiangqi, but trust me, it’s pretty cool. It doesn’t have as many Pawns, and there is a piece called the Canon that can only take if it jumps over another piece on the way.

I’m wondering about making a chinese chess website, to play online. It’s a bit trickier – chinese chess has a 9×10 board (not 8×8), so bit boards will be more complex.

But I might give it a go, building an engine. I think I have the guts of it worked out, although I’m concerned about efficiency…

Chinese Chess Engine