import java.nio.*; import java.nio.channels.*; import java.nio.charset.*; import java.io.*; import java.net.InetSocketAddress; import java.sql.*; import java.util.*; public class Pro2JdbcServer { private Charset charset = null; ServerSocketChannel channel = null; SocketChannel sc = null; Connection con = null; String queryString = null; int portNum; public void start() { ResourceBundle rb = ResourceBundle.getBundle("pro2jdbc"); charset = Charset.forName(rb.getString("fileEncoding")); portNum = Integer.parseInt(rb.getString("portNum")); try { Class.forName("org.postgresql.Driver"); con = DriverManager.getConnection(rb.getString("url"), rb.getString("user"), rb.getString("password")); openChannel(); waitForConnection(); closeChannel(); con.close(); } catch (IOException e) { e.printStackTrace(); } catch(SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } } private void openChannel() throws IOException { channel = ServerSocketChannel.open(); channel.socket().bind(new InetSocketAddress(portNum)); while (!channel.isOpen()) { } System.out.println("Channel is open..."); } private void closeChannel() throws IOException { channel.socket().close(); channel.close(); System.out.println("...channel is closed."); } private void waitForConnection() throws IOException { while (queryString == null || !queryString.equals("END_PROCESSING")) { sc = channel.accept(); if (sc != null) { System.out.println("A connection is added..."); while(processRequest()) { } sc.close(); } } } private boolean processRequest() throws IOException { boolean stillProcessing; ByteBuffer buffer = ByteBuffer.allocate(10000); buffer.clear(); sc.read(buffer); buffer.flip(); queryString = charset.decode(buffer).toString(); System.out.println("Received request: " + queryString); if (queryString == null || queryString.equals("LAST_REQUEST") || queryString.equals("END_PROCESSING")) { stillProcessing = false; } else { stillProcessing = true; try { // execute query Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(queryString); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); // retrieve records while (rs.next()) { String recordAsString = ""; for (int i = 1; i <= numberOfColumns; i++) { String colString = rs.getString(i); if (colString == null) colString = "?"; if (i > 1) recordAsString += "\t"; recordAsString += colString; } writeRecord( recordAsString ); } stmt.close(); } catch(SQLException ex) { writeRecord("SQL_EXCEPTION " + ex.getMessage()); } // write empty record at the end writeRecord( null ); } return stillProcessing; } private void writeRecord( String recordAsString ) throws IOException { /* record size */ ByteBuffer writeBuffer = ByteBuffer.allocate(4); IntBuffer intBuffer = writeBuffer.asIntBuffer(); if (recordAsString == null) { intBuffer.put(0,0); } else { if (recordAsString.equals("")) recordAsString = " "; intBuffer.put(0,recordAsString.length()); } sc.write(writeBuffer); if (recordAsString != null) { /* record itself */ writeBuffer = charset.encode(recordAsString); sc.write(writeBuffer); System.out.println( "Return record: " + recordAsString ); } } public static void main(String[] args) { new Pro2JdbcServer().start(); } }