KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > TCPStreamDatabaseInterface


1 /**
2  * com.mckoi.database.jdbc.TCPStreamDatabaseInterface 16 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.io.*;
28 import java.sql.*;
29 import java.net.*;
30
31 /**
32  * Connection to the database via the TCP protocol.
33  *
34  * @author Tobias Downer
35  */

36
37 class TCPStreamDatabaseInterface extends StreamDatabaseInterface {
38
39   /**
40    * The name of the host we are connected to.
41    */

42   private String JavaDoc host;
43
44   /**
45    * The port we are connected to.
46    */

47   private int port;
48
49   /**
50    * The Socket connection.
51    */

52   private Socket socket;
53
54   /**
55    * Constructor.
56    */

57   TCPStreamDatabaseInterface(String JavaDoc host, int port) {
58     this.host = host;
59     this.port = port;
60   }
61
62   /**
63    * Connects to the database.
64    */

65   void connectToDatabase() throws SQLException {
66     if (socket != null) {
67       throw new SQLException("Connection already established.");
68     }
69     try {
70       // Open a socket connection to the server.
71
socket = new Socket(host, port);
72       // Setup the stream with the given input and output streams.
73
setup(socket.getInputStream(), socket.getOutputStream());
74     }
75     catch (IOException e) {
76       throw new SQLException(e.getMessage());
77     }
78   }
79
80 }
81
Popular Tags