• We just launched and are currently in beta. Join us as we build and grow the community.

Creating User Account Information in Java - Updating and Deleting Records to Database

Ashwathama

OpenAPI Spec Writer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
48
Likes
44
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This is my final continuation of my other two tutorials: Creating User Account Information in Java - Adding Records to Database and Creating User Account Information in Java - Searching Records to Database. Now, we don't have to modify our code for that. Only add the following code below to your UserSettings.java.

1. This is for the initialization of variables to be used as buttons and labels. Just use JButton to have a button in your form. Here we just add Update, Delete, and Exit Button from the previous tutorial.

  1. public

    class

    UserSettings extends

    JFrame

    implements

    ActionListener

    {

  2. JButton

    btnNew =

    new

    JButton

    (

    "Add"

    )

    ;
  3. JButton

    btnUpdate =

    new

    JButton

    (

    "Update"

    )

    ;
  4. JButton

    btnDelete =

    new

    JButton

    (

    "Delete"

    )

    ;
  5. JButton

    btnSearch =

    new

    JButton

    (

    "Search"

    )

    ;
  6. JButton

    btnExit =

    new

    JButton

    (

    "Exit"

    )

    ;
  7. }

2. Now in your constructor UserSettings() , add the 3 buttons for update, delete, and exit.

  1. public

    UserSettings(

    )

    {

  2. btnUpdate.setBounds

    (

    80

    ,190

    ,75

    ,35

    )

    ;
  3. pane.add

    (

    btnUpdate)

    ;
  4. btnUpdate.addActionListener

    (

    this

    )

    ;
  5. btnDelete.setBounds

    (

    155

    ,190

    ,75

    ,35

    )

    ;
  6. pane.add

    (

    btnDelete)

    ;
  7. btnDelete.addActionListener

    (

    this

    )

    ;
  8. btnExit.setBounds

    (

    130

    ,260

    ,75

    ,35

    )

    ;
  9. pane.add

    (

    btnExit)

    ;
  10. }

3. Lastly, in the actionEvents when clicking this 3 buttons put this code below.
  1. if

    (

    source ==

    btnUpdate)

    {
  2. try

    {

  3. String

    uname=

    txtUser.getText

    (

    )

    ;
  4. String

    pass=

    txtPass.getText

    (

    )

    ;
  5. String

    name1=

    txtName1.getText

    (

    )

    ;
  6. String

    name2=

    txtName2.getText

    (

    )

    ;
  7. if

    (

    !

    uname.equals

    (

    ""

    )

    &&

    !

    pass.equals

    (

    ""

    )

    &&

    !

    name1.equals

    (

    ""

    )

    &&

    !

    name2.equals

    (

    ""

    )

    )

    {
  8. st=

    cn.createStatement

    (

    )

    ;
  9. PreparedStatement

    ps =

    cn.prepareStatement

    (

    "UPDATE Login SET password = '"

    +

    txtPass.getText

    (

    )

    +

    "',name1 = '"

    +

    txtName1.getText

    (

    )

    +

    "',name2= '"

    +

    txtName2.getText

    (

    )

    +

    "'WHERE username = '"

    +

    txtUser.getText

    (

    )

    +

    "'"

    )

    ;
  10. ps.executeUpdate

    (

    )

    ;
  11. JOptionPane

    .showMessageDialog

    (

    null

    ,"Account has been successfully updated."

    ,"Payroll System: User settings"

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  12. txtUser.requestFocus

    (

    true

    )

    ;
  13. clear(

    )

    ;
  14. st.close

    (

    )

    ;
  15. }
  16. else

    {
  17. JOptionPane

    .showMessageDialog

    (

    null

    ,"Please Fill Up The Empty Fields"

    ,"Warning"

    ,JOptionPane

    .WARNING_MESSAGE

    )

    ;
  18. }

  19. }

    catch

    (

    SQLException

    y)

    {
  20. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to update!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;
  21. }
  22. }
  23. if

    (

    source==

    btnDelete)

    {
  24. try

    {
  25. PreparedStatement

    ps =

    cn.prepareStatement

    (

    "DELETE FROM Login WHERE username ='"

    +

    txtUser.getText

    (

    )

    +

    "'"

    )

    ;
  26. ps.executeUpdate

    (

    )

    ;
  27. JOptionPane

    .showMessageDialog

    (

    null

    ,"Account has been successfully deleted."

    ,"Payroll System: User settings "

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  28. txtUser.requestFocus

    (

    true

    )

    ;
  29. clear(

    )

    ;
  30. st.close

    (

    )

    ;
  31. }

    catch

    (

    SQLException

    s)

    {
  32. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to delete!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;

    }

  33. }

    if

    (

    source==

    btnExit)

    {
  34. dispose(

    )

    ;
  35. }
  36. }

The syntax for updating a record here is the: PreparedStatement

ps =

cn.prepareStatement

(

"UPDATE Login SET password = '"

+

txtPass.getText

(

)

+

"',name1 = '"

+

txtName1.getText

(

)

+

"',name2= '"

+

txtName2.getText

(

)

+

"'WHERE username = '"

+

txtUser.getText

(

)

+

"'"

)

;

The syntax for deleting a record here is the: PreparedStatement

ps =

cn.prepareStatement

(

"DELETE FROM Login WHERE username ='"

+

txtUser.getText

(

)

+

"'"

)

;

And lastly, the dispose() function is used for exiting or closing the form.

Output:
search1.png

search2.png

search3.png


Here is the full code of this tutorial:

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;
  3. import

    java.awt.event.*

    ;
  4. import

    java.sql.*

    ;
  5. import

    java.lang.*

    ;

  6. public

    class

    UserSettings extends

    JFrame

    implements

    ActionListener

    {

  7. JLabel

    lblUser =

    new

    JLabel

    (

    "Username "

    ,JLabel

    .RIGHT

    )

    ;
  8. JLabel

    lblPass =

    new

    JLabel

    (

    "Password "

    ,JLabel

    .RIGHT

    )

    ;
  9. JLabel

    lblName1 =

    new

    JLabel

    (

    "First Name"

    ,JLabel

    .RIGHT

    )

    ;
  10. JLabel

    lblName2 =

    new

    JLabel

    (

    "Family Name"

    ,JLabel

    .RIGHT

    )

    ;

  11. JTextField

    txtUser =

    new

    JTextField

    (

    20

    )

    ;
  12. JPasswordField

    txtPass=

    new

    JPasswordField

    (

    20

    )

    ;

    lll
  13. JTextField

    txtName1=

    new

    JTextField

    (

    20

    )

    ;
  14. JTextField

    txtName2=

    new

    JTextField

    (

    20

    )

    ;

  15. JButton

    btnNew =

    new

    JButton

    (

    "Add"

    )

    ;
  16. JButton

    btnUpdate =

    new

    JButton

    (

    "Edit"

    )

    ;
  17. JButton

    btnDelete =

    new

    JButton

    (

    "Delete"

    )

    ;
  18. JButton

    btnSearch =

    new

    JButton

    (

    "Search"

    )

    ;
  19. JButton

    btnExit =

    new

    JButton

    (

    "Exit"

    )

    ;

  20. Connection

    cn;
  21. Statement

    st;
  22. PreparedStatement

    ps;
  23. public

    void

    clear(

    )

    {
  24. txtUser.setText

    (

    ""

    )

    ;
  25. txtPass.setText

    (

    ""

    )

    ;
  26. txtName1.setText

    (

    ""

    )

    ;
  27. txtName2.setText

    (

    ""

    )

    ;
  28. }
  29. public

    UserSettings(

    )

    {
  30. super

    (

    "User Account Settings"

    )

    ;

  31. JPanel

    pane =

    new

    JPanel

    (

    )

    ;
  32. pane.setLayout

    (

    null

    )

    ;

  33. lblUser.setBounds

    (

    5

    ,50

    ,80

    ,25

    )

    ;
  34. pane.add

    (

    lblUser)

    ;
  35. txtUser.setBounds

    (

    90

    ,50

    ,150

    ,25

    )

    ;
  36. pane.add

    (

    txtUser)

    ;
  37. lblUser.setForeground

    (

    Color

    .white

    )

    ;

  38. lblPass.setBounds

    (

    5

    ,85

    ,80

    ,25

    )

    ;
  39. pane.add

    (

    lblPass)

    ;
  40. txtPass.setBounds

    (

    90

    ,85

    ,150

    ,25

    )

    ;
  41. txtPass.setEchoChar

    (

    '*'

    )

    ;
  42. pane.add

    (

    txtPass)

    ;
  43. lblPass.setForeground

    (

    Color

    .white

    )

    ;

  44. lblName1.setBounds

    (

    5

    ,120

    ,80

    ,25

    )

    ;
  45. pane.add

    (

    lblName1)

    ;
  46. txtName1.setBounds

    (

    90

    ,120

    ,150

    ,25

    )

    ;
  47. pane.add

    (

    txtName1)

    ;
  48. lblName1.setForeground

    (

    Color

    .white

    )

    ;


  49. lblName2.setBounds

    (

    5

    ,155

    ,80

    ,25

    )

    ;
  50. pane.add

    (

    lblName2)

    ;
  51. txtName2.setBounds

    (

    90

    ,155

    ,150

    ,25

    )

    ;
  52. pane.add

    (

    txtName2)

    ;
  53. lblName2.setForeground

    (

    Color

    .white

    )

    ;

  54. btnNew.setBounds

    (

    5

    ,190

    ,75

    ,35

    )

    ;
  55. pane.add

    (

    btnNew)

    ;

  56. btnNew.addActionListener

    (

    this

    )

    ;
  57. btnUpdate.setBounds

    (

    80

    ,190

    ,75

    ,35

    )

    ;
  58. pane.add

    (

    btnUpdate)

    ;

  59. btnUpdate.addActionListener

    (

    this

    )

    ;
  60. btnDelete.setBounds

    (

    155

    ,190

    ,75

    ,35

    )

    ;
  61. pane.add

    (

    btnDelete)

    ;

  62. btnDelete.addActionListener

    (

    this

    )

    ;
  63. btnSearch.setBounds

    (

    230

    ,190

    ,75

    ,35

    )

    ;
  64. pane.add

    (

    btnSearch)

    ;

  65. btnSearch.addActionListener

    (

    this

    )

    ;
  66. btnExit.setBounds

    (

    130

    ,260

    ,75

    ,35

    )

    ;
  67. pane.add

    (

    btnExit)

    ;

  68. pane.setBackground

    (

    Color

    .black

    )

    ;

  69. btnExit.addActionListener

    (

    this

    )

    ;
  70. setContentPane(

    pane)

    ;
  71. setDefaultCloseOperation(

    JFrame

    .DISPOSE_ON_CLOSE

    )

    ;
  72. pane.setBorder

    (

    BorderFactory

    .createTitledBorder

    (
  73. BorderFactory

    .createEtchedBorder

    (

    )

    , "Creating User Account"

    )

    )

    ;

  74. try

    {
  75. Class

    .forName

    (

    "sun.jdbc.odbc.JdbcOdbcDriver"

    )

    ;
  76. cn =

    DriverManager

    .getConnection

    (

    "jdbc:odbc:User"

    )

    ;
  77. }

    catch

    (

    ClassNotFoundException

    e)

    {
  78. System

    .err

    .println

    (

    "Failed to load driver"

    )

    ;
  79. e.printStackTrace

    (

    )

    ;
  80. }
  81. catch

    (

    SQLException

    e)

    {
  82. System

    .err

    .println

    (

    "Unable to connect"

    )

    ;
  83. e.printStackTrace

    (

    )

    ;
  84. }
  85. }

  86. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  87. Object

    source =

    e.getSource

    (

    )

    ;
  88. if

    (

    source ==

    btnNew)

    {
  89. try

    {
  90. String

    uname=

    txtUser.getText

    (

    )

    ;
  91. String

    pass=

    txtPass.getText

    (

    )

    ;
  92. String

    name1=

    txtName1.getText

    (

    )

    ;
  93. String

    name2=

    txtName2.getText

    (

    )

    ;
  94. if

    (

    !

    uname.equals

    (

    ""

    )

    &&

    !

    pass.equals

    (

    ""

    )

    &&

    !

    name1.equals

    (

    ""

    )

    &&

    !

    name2.equals

    (

    ""

    )

    )

    {
  95. st=

    cn.createStatement

    (

    )

    ;
  96. ps=

    cn.prepareStatement

    (

    "INSERT INTO Login"

    +

    " (username,password,name1,name2) "

    +

    " VALUES(?,?,?,?)"

    )

    ;
  97. ps.setString

    (

    1

    ,txtUser.getText

    (

    )

    )

    ;
  98. ps.setString

    (

    2

    ,txtPass.getText

    (

    )

    )

    ;
  99. ps.setString

    (

    3

    ,txtName1.getText

    (

    )

    )

    ;
  100. ps.setString

    (

    4

    ,txtName2.getText

    (

    )

    )

    ;
  101. ps.executeUpdate

    (

    )

    ;
  102. JOptionPane

    .showMessageDialog

    (

    null

    ,"New account has been successfully added."

    ,"Payroll System: User settings"

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  103. txtUser.requestFocus

    (

    true

    )

    ;
  104. st.close

    (

    )

    ;
  105. clear(

    )

    ;
  106. }
  107. else

    {
  108. JOptionPane

    .showMessageDialog

    (

    null

    ,"Please Fill Up The Empty Fields"

    ,"Warning"

    ,JOptionPane

    .WARNING_MESSAGE

    )

    ;
  109. }
  110. }

    catch

    (

    SQLException

    sqlEx)

    {
  111. sqlEx.printStackTrace

    (

    )

    ;
  112. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to save!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;

    }
  113. }
  114. if

    (

    source ==

    btnSearch)

    {
  115. try

    {

  116. String

    sUser =

    ""

    ;
  117. int

    tmp=

    0

    ;
  118. clear(

    )

    ;
  119. sUser =

    JOptionPane

    .showInputDialog

    (

    null

    ,"Enter Username to search."

    ,"Payroll System: User settings"

    ,JOptionPane

    .QUESTION_MESSAGE

    )

    ;
  120. st=

    cn.createStatement

    (

    )

    ;
  121. ResultSet

    rs=

    st.executeQuery

    (

    "SELECT * FROM Login WHERE username = '"

    +

    sUser +

    "'"

    )

    ;

  122. while

    (

    rs.next

    (

    )

    )

    {
  123. txtUser.setText

    (

    rs.getString

    (

    1

    )

    )

    ;
  124. txtPass.setText

    (

    rs.getString

    (

    2

    )

    )

    ;
  125. txtName1.setText

    (

    rs.getString

    (

    3

    )

    )

    ;
  126. txtName2.setText

    (

    rs.getString

    (

    4

    )

    )

    ;
  127. tmp=

    1

    ;
  128. }
  129. st.close

    (

    )

    ;
  130. if

    (

    tmp==

    0

    )

    {
  131. JOptionPane

    .showMessageDialog

    (

    null

    ,"No record found!!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  132. }
  133. }

    catch

    (

    SQLException

    s)

    {
  134. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to search!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;
  135. System

    .out

    .println

    (

    "SQL Error"

    +

    s.toString

    (

    )

    +

    " "

    +

    s.getErrorCode

    (

    )

    +

    " "

    +

    s.getSQLState

    (

    )

    )

    ;
  136. }
  137. }
  138. if

    (

    source ==

    btnUpdate)

    {
  139. try

    {

  140. String

    uname=

    txtUser.getText

    (

    )

    ;
  141. String

    pass=

    txtPass.getText

    (

    )

    ;
  142. String

    name1=

    txtName1.getText

    (

    )

    ;
  143. String

    name2=

    txtName2.getText

    (

    )

    ;
  144. if

    (

    !

    uname.equals

    (

    ""

    )

    &&

    !

    pass.equals

    (

    ""

    )

    &&

    !

    name1.equals

    (

    ""

    )

    &&

    !

    name2.equals

    (

    ""

    )

    )

    {
  145. st=

    cn.createStatement

    (

    )

    ;
  146. PreparedStatement

    ps =

    cn.prepareStatement

    (

    "UPDATE Login SET password = '"

    +

    txtPass.getText

    (

    )

    +

    "',name1 = '"

    +

    txtName1.getText

    (

    )

    +

    "',name2= '"

    +

    txtName2.getText

    (

    )

    +

    "'WHERE username = '"

    +

    txtUser.getText

    (

    )

    +

    "'"

    )

    ;
  147. ps.executeUpdate

    (

    )

    ;
  148. JOptionPane

    .showMessageDialog

    (

    null

    ,"Account has been successfully updated."

    ,"Payroll System: User settings"

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  149. txtUser.requestFocus

    (

    true

    )

    ;
  150. clear(

    )

    ;
  151. st.close

    (

    )

    ;
  152. }
  153. else

    {
  154. JOptionPane

    .showMessageDialog

    (

    null

    ,"Please Fill Up The Empty Fields"

    ,"Warning"

    ,JOptionPane

    .WARNING_MESSAGE

    )

    ;
  155. }

  156. }

    catch

    (

    SQLException

    y)

    {
  157. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to update!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;
  158. }
  159. }
  160. if

    (

    source==

    btnDelete)

    {
  161. try

    {
  162. PreparedStatement

    ps =

    cn.prepareStatement

    (

    "DELETE FROM Login WHERE username ='"

    +

    txtUser.getText

    (

    )

    +

    "'"

    )

    ;
  163. ps.executeUpdate

    (

    )

    ;
  164. JOptionPane

    .showMessageDialog

    (

    null

    ,"Account has been successfully deleted."

    ,"Payroll System: User settings "

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  165. txtUser.requestFocus

    (

    true

    )

    ;
  166. clear(

    )

    ;
  167. st.close

    (

    )

    ;
  168. }

    catch

    (

    SQLException

    s)

    {
  169. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to delete!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;

    }

  170. }

    if

    (

    source==

    btnExit)

    {
  171. dispose(

    )

    ;
  172. }
  173. }
  174. // public void frameUser(){
  175. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  176. UserSettings panel =

    new

    UserSettings(

    )

    ;
  177. panel.setSize

    (

    370

    ,350

    )

    ;
  178. panel.setVisible

    (

    true

    )

    ;
  179. panel.setLocation

    (

    350

    ,200

    )

    ;
  180. panel.setResizable

    (

    false

    )

    ;
  181. }
  182. }

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add and Follow me on Facebook: https://www.facebook.com/donzzsky


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,186

324,194

Top