My task is to read the tag number when I scan a tag in the RFID reader, and take that number to match with an identical number in the database. Then, I extract the data in the same row as that matched number.
E.g. I have scanned in the tag with tag number "4400E6EF1A57" and it matches with the attribute "usernum" in my databse.
The problem I am currently having is that I cannot connect to the database. I suspect there is a problem with "Connection con". Now I keep receiving the "no connection" exception.
Here is my code. Class Usercon is used to define the variables in the database and prints out the data. Class trying5 is for reading the RFID tag.
import java.sql.*;
import java.util.*;
public class usercon{
int usernum;
int sports;
int books;
int music;
int technology;
int food;
int fitness;
String idNum;
Connection con;
public usercon(Connection connect, String idNum){
this.con = connect;
this.idNum = idNum;
//Connection connection = null;
}
public void displayadvertuser(){
try{
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM advertuser");
while(rs.next()){
if(idNum == rs.getString(usernum)){
break;
}
}
System.out.println(rs.getInt(books)+ rs.getInt(fitness)+ rs.getInt(food)+ rs.getInt(music)+ rs.getInt(sports)+ rs.getInt(technology));
con.createStatement().close();
}
catch (SQLException sq) {
sq.printStackTrace();
}
}
}
import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;
public class trying5 implements Runnable, SerialPortEventListener {
static Enumeration portList;
static CommPortIdentifier portId;
SerialPort serialPort;
InputStream inputStream;
Thread readThread;
Connection con;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
trying5 reader = new trying5();
}
}
}
}
public trying5() {
try {
// Load the JDBC driver
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ug", "ora_l6k7@ug", "a16373937");
} catch (SQLException e) {
// Could not connect to the database
System.out.println("no connection");
}
try {
serialPort = (SerialPort) portId.open("trying5Application", 2000);
}
catch (PortInUseException e)
{
System.out.println(e);
}
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e)
{
System.out.println(e);
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e)
{
System.out.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_ODD);
}
catch (UnsupportedCommOperationException e)
{
System.out.println(e);
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
// print to console
try {
byte[] buf = new byte[12];
int len = inputStream.read(buf,0,buf.length);
if (len != buf.length ) {
throw new RuntimeException("the stream is closed");
}
String newtuple = new String(buf);
System.out.println();
// System.out.print(newtuple+ "\t");
if(newtuple.equals(new String("4400E6EF1A57"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6EB6029"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6ACDFD1"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6E6E0A4"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6C0FA98"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6FF6D30"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
} catch (IOException e)
{
System.out.println(e);
}
break;
}
}
}
E.g. I have scanned in the tag with tag number "4400E6EF1A57" and it matches with the attribute "usernum" in my databse.
The problem I am currently having is that I cannot connect to the database. I suspect there is a problem with "Connection con". Now I keep receiving the "no connection" exception.
Here is my code. Class Usercon is used to define the variables in the database and prints out the data. Class trying5 is for reading the RFID tag.
import java.sql.*;
import java.util.*;
public class usercon{
int usernum;
int sports;
int books;
int music;
int technology;
int food;
int fitness;
String idNum;
Connection con;
public usercon(Connection connect, String idNum){
this.con = connect;
this.idNum = idNum;
//Connection connection = null;
}
public void displayadvertuser(){
try{
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM advertuser");
while(rs.next()){
if(idNum == rs.getString(usernum)){
break;
}
}
System.out.println(rs.getInt(books)+ rs.getInt(fitness)+ rs.getInt(food)+ rs.getInt(music)+ rs.getInt(sports)+ rs.getInt(technology));
con.createStatement().close();
}
catch (SQLException sq) {
sq.printStackTrace();
}
}
}
import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;
public class trying5 implements Runnable, SerialPortEventListener {
static Enumeration portList;
static CommPortIdentifier portId;
SerialPort serialPort;
InputStream inputStream;
Thread readThread;
Connection con;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
trying5 reader = new trying5();
}
}
}
}
public trying5() {
try {
// Load the JDBC driver
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ug", "ora_l6k7@ug", "a16373937");
} catch (SQLException e) {
// Could not connect to the database
System.out.println("no connection");
}
try {
serialPort = (SerialPort) portId.open("trying5Application", 2000);
}
catch (PortInUseException e)
{
System.out.println(e);
}
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e)
{
System.out.println(e);
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e)
{
System.out.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_ODD);
}
catch (UnsupportedCommOperationException e)
{
System.out.println(e);
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
// print to console
try {
byte[] buf = new byte[12];
int len = inputStream.read(buf,0,buf.length);
if (len != buf.length ) {
throw new RuntimeException("the stream is closed");
}
String newtuple = new String(buf);
System.out.println();
// System.out.print(newtuple+ "\t");
if(newtuple.equals(new String("4400E6EF1A57"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6EB6029"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6ACDFD1"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6E6E0A4"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6C0FA98"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
else if(newtuple.equals(new String("4400E6FF6D30"))){
usercon newcon = new usercon(con, newtuple);
System.out.print(newtuple);
newcon.displayadvertuser();
}
} catch (IOException e)
{
System.out.println(e);
}
break;
}
}
}