0

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");
    }
}    
ppeterka
  • 20,583
  • 6
  • 63
  • 78
Gelos
  • 27
  • 2
  • A friendly advice: please use PreparedStatements. Concatenating SQL queries is very wrong... Just google SQL injection. Also: please read up on the Java naming convention. Your code is not adhering to it. – ppeterka Aug 13 '16 at 13:32
  • What is `jdbc:ucanaccess` in your connection ? – Inzimam Tariq IT Aug 13 '16 at 13:55
  • @InzimamTariqIT That is a JDBC driver named "UCanAccess". It is to access MS Access databases. – ppeterka Aug 13 '16 at 13:58
  • 2
    Please [edit] your question to better explain your problem. The title currently seems to suggest that you can see the inserted data *somehow*, but you can't see the inserted data when you try to view it some other way. A detailed description of the steps to reproduce the issue will greatly increase the chances of your receiving meaningful assistance. – Gord Thompson Aug 13 '16 at 17:43

0 Answers0