~ubuntu-branches/ubuntu/maverick/ntop/maverick

« back to all changes in this revision

Viewing changes to ntop/database/ODBCServer.java

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import java.util.*;
 
2
import java.io.*;
 
3
import java.net.*;
 
4
import java.sql.*;
 
5
 
 
6
public class ODBCServer {
 
7
  private Connection connection;
 
8
 
 
9
  private boolean checkForWarning (SQLWarning warn) {
 
10
    boolean rc = false;
 
11
 
 
12
    // If a SQLWarning object was given, display the
 
13
    // warning messages.  Note that there could be
 
14
    // multiple warnings chained together
 
15
    try {
 
16
      if (warn != null) {
 
17
        System.out.println ("\n *** Warning ***\n");
 
18
        rc = true;
 
19
        while (warn != null) {
 
20
          System.out.println ("SQLState: " +
 
21
                              warn.getSQLState ());
 
22
          System.out.println ("Message:  " +
 
23
                              warn.getMessage ());
 
24
          System.out.println ("");
 
25
          warn = warn.getNextWarning ();
 
26
        }
 
27
      }
 
28
      return rc;
 
29
    } catch(Exception e) {
 
30
      return(true);
 
31
    }
 
32
  }
 
33
 
 
34
  public ODBCServer(String dbName) {
 
35
    try {
 
36
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
37
        
 
38
      connection = DriverManager.getConnection("jdbc:odbc:NTOP");
 
39
      checkForWarning(connection.getWarnings());
 
40
      //connection.setAutoCommit(true); // Automatic transaction commitment
 
41
    } catch (Exception e) {
 
42
      e.printStackTrace();
 
43
      System.exit(-1);
 
44
    }
 
45
  }
 
46
 
 
47
  public void executeStatement(String statement) {
 
48
       PreparedStatement stmt;
 
49
       int rc;
 
50
       
 
51
        try {
 
52
          stmt = connection.prepareStatement(statement); // Insert record in the HostInfo table
 
53
          rc = stmt.executeUpdate();
 
54
          stmt.close(); // Close the statement
 
55
        } catch(Exception ex) {
 
56
          ex.printStackTrace();
 
57
        }
 
58
  }
 
59
  
 
60
  
 
61
  public static void main(String args[])
 
62
  {
 
63
        byte buffer[];
 
64
        int bufLength = 1514;
 
65
        ODBCServer odbcServer;
 
66
        DatagramSocket socket = null;
 
67
        DatagramPacket pkt;
 
68
        
 
69
        System.out.println("Starting up...");
 
70
        
 
71
        try {
 
72
            socket = new DatagramSocket(4000);
 
73
        } catch (Exception e) {
 
74
          e.printStackTrace();
 
75
          System.exit(-1);
 
76
        }
 
77
       
 
78
        buffer = new byte[bufLength];
 
79
        pkt = new DatagramPacket(buffer, buffer.length);
 
80
        odbcServer = new ODBCServer("NTOP");
 
81
        
 
82
        while(true) {
 
83
            try {
 
84
               socket.receive(pkt);    
 
85
               String statement = new String(buffer, 0, pkt.getLength());
 
86
               
 
87
               System.out.println("["+pkt.getLength()+"] '"+statement+"'");  
 
88
               
 
89
               odbcServer.executeStatement(statement);
 
90
               
 
91
               pkt.setLength(bufLength);
 
92
            } catch (Exception e) {
 
93
              e.printStackTrace();
 
94
            }
 
95
        }
 
96
 
 
97
        /*
 
98
        socket.close();
 
99
        System.out.println("Done.");
 
100
        */
 
101
  }
 
102
 
 
103
}