I'm using the code
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
to output: 00-27-0E-C2-53-B7
I need this output to be stored in a variable and I need a开发者_JS百科 query to save it to a MySQL database. I also want to fetch MAC addresses automatically on my login page along with user details.
That way, I can store users' MAC addresses along with their usernames and passwords in the database. The idea is that, when a user logs in, I want to be able to fetch the MAC address automatically to authenticate the user.
How can I do this?
You are asking a lot of questions.
Your mac address is already stored in variable. Array mac[] is a array variable. If you need separate variable just define it like the following:
String myMac = mac[i];
Saving data in DB. I believe that you are already using DB. If for exampel you are using plain JDBC construct
insert
orupdate
SQL statement like this: insert into UserData ('mac') VAULUES (?) where user_id=? Obviously the concrete fields depend on your DB schema. If you are using some ORM system ask more specific question about this ORM. But in most cases this will be even simpler. If for example you already have class User:class User { private String username; private String password; // etc }
...just add the new field mac
there:
class User {
private String username;
private String password;
private String mac;
// etc
}
If you are using JPA your DB schema will be updated automatically and the data will be saved there too.
- The same is about login page. If you already have login page that shows for example user ID, add similar code for MAC
etc, etc....
The zen of Python says "simple is better than complex."
This code is from SO user Carles Barrobes:
public String obtainMacAddress() throws Exception
{
Process aProc = Runtime.getRuntime().exec("ipconfig /all");
InputStream procOut = new DataInputStream(aProc.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(procOut));
String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
Pattern aPatternMac = Pattern.compile(aMacAddress);
String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
Pattern aPatternIp = Pattern.compile(aIpAddress);
String aNewAdaptor = "[A-Z].*$";
Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);
// locate first MAC address that has IP address
boolean zFoundMac = false;
boolean zFoundIp = false;
String foundMac = null;
String theGoodMac = null;
String strLine;
while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
if (aMatcherNewAdaptor.matches()) {
zFoundMac = zFoundIp = false;
}
Matcher aMatcherMac = aPatternMac.matcher(strLine);
if (aMatcherMac.find()) {
foundMac = aMatcherMac.group(0);
zFoundMac = true;
}
Matcher aMatcherIp = aPatternIp.matcher(strLine);
if (aMatcherIp.matches()) {
zFoundIp = true;
if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
}
}
aProc.destroy();
aProc.waitFor();
return theGoodMac;}
Note that it is necessary to have an ethernet or wifi connection to run the above.
精彩评论