无线传感器网络操作系统源代码

源代码在线查看: dbreader.java

软件大小: 1076 K
上传用户: liuhong22008
关键词: 无线传感器 网络操作系统 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				package net.tinyos.SerialForwarder;								import java.sql.*;				import java.util.*;								public class DBReader {								    Connection db = null;				    Statement  st = null;				    private static final String urlMYSQL = "jdbc:mysql://127.0.0.1:3306/packets";				    private static final String urlPSQL= "jdbc:postgresql://127.0.0.1:16882/motes";				    private String m_usr = "bwhull";				    private String m_pwd = "";				    private boolean POSTGRESQL = true;				    public static final boolean DEBUG_QUERIES = true;				    public static final boolean DEBUG         = true;				    public static final int FIELD1_POS = 1;				    public static final int FIELD2_POS = 2;				    public static final int FIELD3_POS = 3;								    private ResultSet m_rsPackets = null;								    public DBReader( String user, String pass, boolean ps ) {					m_usr = user;					m_pwd = pass;					POSTGRESQL = ps;				    }								    public boolean Connect ( ) {					try {					    System.out.println ("Connecting to database");									    if ( POSTGRESQL ) {						Class.forName ( "org.postgresql.Driver" );						db = DriverManager.getConnection(urlPSQL, m_usr, m_pwd);					    } else {						Class.forName("org.gjt.mm.mysql.Driver");						db = DriverManager.getConnection(urlMYSQL, m_usr, m_pwd);					    }					    db.setAutoCommit (false);					    //st = db.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,	    ResultSet.CONCUR_UPDATABLE );					    st = db.createStatement ( );					    return true;					} catch (Exception ex) {					    ex.printStackTrace();					    return false;					}				    }								    public void Close ( ) {					try {					    if ( st != null ) { st.close ( ); }					    if ( db != null ) { db.close ( ); }					    st = null;					    db = null;					} catch (Exception e ) { }				    }								    public byte[] NextPacket (  ) {					System.out.println ( "DBReader: getting next packet");					if ( st == null ) {					    System.out.println ( "DBReader: Unable to write packet\n");					    return null;					}									byte[] packet = null;									try {					    if ( m_rsPackets == null ) {						System.out.println ("DBReader: executing read query");						m_rsPackets = st.executeQuery ( "SELECT * FROM packets ORDER BY rcvdtime asc");					    }					    if ( m_rsPackets != null && m_rsPackets.next() ) {						System.out.println ("DBReader: extracting payload");						packet = m_rsPackets.getBytes ( "payload" );					    }					}					catch ( SQLException e ) {					    e.printStackTrace ( );					    return null;					}					return packet;				    }								    public Timestamp GetTimestamp ( ) {									if ( m_rsPackets == null ) {					    System.out.println ( "DBReader: must first read a packet\n");					    return null;					}									Timestamp ts = null;									try {					    if ( m_rsPackets != null ) {						ts = m_rsPackets.getTimestamp ( "rcvdTime" );					    }					}					catch ( SQLException e ) {					    e.printStackTrace ();					    return null;					}									return ts;				    }										}							

相关资源