2014-11-24 56 views
-1

有幾個關於我拼湊在一起的代碼的問題。加密純文本密碼並將變量傳遞給xml以顯示在android

首先,我如何加密密碼變量,以便它不以純文本格式存儲?

其次,我可以將'第一'&'最後'的變量傳遞給一個要顯示在android設備上的XML元素嗎?我如何才能最好地獲得要在Android屏幕上打印的「第一」,「最後」變量?

爲了清楚我沒有得到此代碼的工作,我只是想知道我可以如何使用android應用程序的輸出。

import java.sql.*; 

public class SQL{ 

public static void main(String[] args) { 
    /* 
    Variables that store our login information and SQL driver information. 
    url = database url 
    dbName = name of table to query 
    driver = name of MySQL driver 
    userName = username for SQL server 
    password = password for SQL server 
    */ 
    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "testdb"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "****"; 
    String password = "****"; 

    //Start our exception handling block. 
     try { 
      //Connect to the driver? I honestly have no idea what this does. 
      Class.forName(driver).newInstance(); 
      //Establish a connection to the SQL server 
      Connection conn = DriverManager.getConnection(url+dbName,userName,password); 
      //Create a statement to be executed. 
      Statement stmt = conn.createStatement(); 
      //The query that will be executed. 
      String sql = "SELECT * FROM Achievments"; 
      //Create a results set variable that stores the output of our query. 
      ResultSet rs = stmt.executeQuery(sql); 

      //Start a while loop that executes while there are still results in the table? 
      while(rs.next()){ 
       //Retrieve by column name 
       String first = rs.getString("ach_names"); 
       String last = rs.getString("ach_text"); 

       //Display values 
       System.out.print(first); 
       System.out.println(last); 
      } 
      rs.close(); 
     }catch(SQLException se){ 
      //Handle errors for JDBC 
      se.printStackTrace(); 
     }catch(Exception e){ 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

要不用純文本存儲密碼,您可以使用md5。事情是這樣的:

public static String md5(String string) { 
    byte[] hash; 

    try { 
     hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); 
    } catch (NoSuchAlgorithmException e) { 
     throw new RuntimeException("Huh, MD5 should be supported?", e); 
    } catch (UnsupportedEncodingException e) { 
     throw new RuntimeException("Huh, UTF-8 should be supported?", e); 
    } 

    StringBuilder hex = new StringBuilder(hash.length * 2); 

    for (byte b : hash) { 
     int i = (b & 0xFF); 
     if (i < 0x10) hex.append('0'); 
     hex.append(Integer.toHexString(i)); 
    } 

    return hex.toString(); 
} 

關於firstlast變量,你這是什麼意思pass to an xml element to be displayed?如果你想以編程方式設置一個TextView的文本,你可以這樣做:

TextView yourTextView = (TextView)findViewById(R.id.yourTextView); 
yourTextView.setText(first); //or `last` in another textview 

但也許我不明白你的意思這個first and last variables什麼。

希望這個答案的第一部分有所幫助。

相關問題