Monday, 24 November 2014

Encrypting plain-text passwords and passing variables to xml for display in android



Got a couple of questions about this bit of Java code I've cobbled together.


First, how can I encrypt the password variable so that it's not stored in plain text?


Second, can I pass the 'first' & 'last' variables to an XML element to be displayed on an android device? How would I best go about getting the 'first', 'last' variables to be printed on an android screen?


To be clear I am having no trouble getting this code to work, I just want to know how I can use the output in an android app.



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 = "<insert username for SQL server here>";
String password = "<insert password for SQL server here>";



//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();
}






}
}

No comments:

Post a Comment