• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My coding style is an utter mess

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I struggle to learn and keep up with the tests and assignments while having a full-time job completely detached from the IT field, I realized that I am cutting corners.
My coding style is an utter mess. If my code works, I spend very little time polishing it, and I feel like I am missing some basic OOP concepts.
Here is a recent assignment. It is just a program that communicates with a MySQL database and allows an imaginary company to store and alter data of its employees.
I would be extremely grateful is someone could point out any bad practices and improvements I could make. Especially the connection part. I feel like I should have make a separate class for that.

(Please note that in order to make it in time, I rarely checked the user input, if at all.)





This is the class where all the crud operations are



Rest of the code coming right up.
 
Amadeus Pan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the main window. I made it using the NetBeans designer. I have put in bold the relevant code and deleted the generated code.


 
Amadeus Pan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now for each operation other than filtering and populating the table with DB data, I made a separate frame, I have not included here all the generated code,
but only the action listeners and the components I added to it.


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would be extremely grateful is someone could point out any bad practices and improvements I could make. Especially the connection part. I feel like I should have make a separate class for that.


Your instincts here are correct: you definitely need a DBConnection class.  It can be very small, just a static field the gets the connect, some constants for the connection HTTP, use, etc., and a static getConnection() method that returns the connections object.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a fair bit of code repetition that should be consolidated. Consider...
This block is repeated several times. You might consider adding an Employee constructor that takes a ResultSet. Then you might also consider adding a static method to your DBCrud class.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also consider the formatting of that code. Look at the lengths of the lines, and the spacing between methods. We have some suggestions here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic