1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| package pos.fonction;
import java.util.List;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.smartcardio.*;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class TagScan extends JFrame{
private String uid;
private boolean valid=false;
Statement state;
ResultSet result;
private double montant;
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length * 2) + "X", new BigInteger(1,data));
}
public TagScan(JFrame frame,Connection conn) {
NfcDisconnect disc = new NfcDisconnect(frame);
disc.setVisible(false);
JLabel lab = new JLabel();
JPanel pan7 = new JPanel();
pan7.removeAll();
pan7.add(lab);
pan7.add(new JLabel(new ImageIcon("image/NfcConn.png")));
pan7.updateUI();
this.setTitle("NFC Tag");
//Définit sa taille : 400 pixels de large et 200 pixels de haut
this.setBounds(0,0,400, 200);
//La position
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
//La boîte ne devra pas être redimensionnable
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.getContentPane().add(pan7);
this.setUndecorated(false);
//Enfin on l'affiche
this.setVisible(true);
boolean check = false;
do {
try {
// Display the list of terminals
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
// Use the first terminal
CardTerminal terminal = terminals.get(0);
// Connect with the card
if(check==true)
{
disc.setVisible(false);
check=false;
}
terminal.waitForCardPresent(0);
Card card = terminal.connect("*");
System.out.println("Card: " + card);
CardChannel channel = card.getBasicChannel();
// Send test command
ResponseAPDU response = channel.transmit(new CommandAPDU( new byte[] { (byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
System.out.println("Response: " + response.toString());
if (response.getSW1() == 0x63 && response.getSW2() == 0x00) System.out.println("Failed");
System.out.println("UID: " + bin2hex(response.getData()));
uid=bin2hex(response.getData());
terminal.waitForCardAbsent(0);
// Disconnect the card
card.disconnect(false);
} catch(Exception e) {
System.out.println("Ouch: " + e.toString());
if(check==false)
{
disc.setVisible(true);
check=true;
}
}
try {
state = conn.createStatement();
result = state.executeQuery("SELECT montant FROM compte WHERE code_barre='"+uid+"'");
while(result.next()){
setMontant(result.getDouble("montant"));
valid=true;
}
result.close();
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while(valid==false);
this.dispose();
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public boolean getValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public double getMontant() {
return montant;
}
public void setMontant(double montant) {
this.montant = montant;
}
} |
Partager