Below is code to my UserArray which connects to my database and has a method to insert a new user based off data taken from a gui i created (when a create button is clicked) [see second bit of code]
public class UserArray
{
private Connection conn = null;
public UserArray(String db)
{
try
{
String filename = db;
conn = DriverManager.getConnection("jdbc:ucanaccess://" + filename,"","");
}
catch (Exception e)
{
System.out.println("Error: " + e);
}
}
public void InsertNewUser (String fn, String ln, String hn, String sn, String cn,
String pc, String un, String pass)
{
try
{
Statement st = conn.createStatement();
String sql = "INSERT INTO Users (Username, Password, FirstName, LastName, "
+ "HouseNumber, StreetName, CityName, PostalCode) "
+ "VALUES ('"+un+"', '"+pass+"', '"+fn+"', '"+ln+"', '"+hn+"','"+sn+"', '"+cn+"', '"+pc+"')";
st.execute(sql);
}
catch (SQLException se)
{
System.out.println("ERROR " + se);
}
}
}
Below is the code for my GUI when the create button is 'hit' once all details have been entered
private void btnCreateActionPerformed(java.awt.event.ActionEvent evt)
{
String fn = txtFirstName.getText(); //new user enters their first name
String ln = txtLastName.getText(); //new user enters their last name
String hn = txtHouseNum.getText(); //new user enters their house number
String sn = txtStreetName.getText(); //new user enters their street name
String cn = txtCity.getText(); //new user enters their city name
String pc = txtPostCode.getText(); //new user enters their postal code
String un = txtUsername.getText(); //new user enters their username
String pass = pwdPassword.getText(); //new user enters their password
String repass = pwdREPassword.getText(); //new user re-enters their password
UserArray ua = new UserArray("kickZA.accdb");
if (repass.equals(pass))
{
ua.InsertNewUser(fn, ln, hn, sn, cn, pc, un, pass);
}
else
{
JOptionPane.showMessageDialog(this, "passwords to not match");
}
}