Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java form to MySQL

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, this is abit of a broad question but i'll try to make it as concentrated as possible. What i'm trying to do as a fun little project is to make a form using java and then when all the information is entered into the form and submit is clicked I want it to be stored inside a database. At the moment i've created a database using MySQL and i've created a class within the project which connects from the project to the database and it works fine. But my issue is I can't find any tutorials or anything online that helps me do what I want. Can someone push me in the right direction? I've searched everywhere and I can't find anything.

 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about a form in a browser?
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mitchell,

You have created Just a Connection class.

Steps to create a working project to Display form which will contain all details which will be submitted to Database and stored,
(Considering you want to create a Web Application using Java/JSP/Servlet and MySQL)
1. HTML Form
2. Data Access Object and POJO (Data Getter/Setter Object)
3. Data Connection Object
4. Processing Page


--------------------------------------  Detail Page -- HTML Page -- UI


----------------------------------------------------------- Data Access Object ---- JAVA --



------------------------------- POJO (Getter/Setter Object for User )-------



---------------------- Processing Page --------------  // If you are using Servlet , there are different ways with JSP

---------------

Hope this helps with your question.

Happy Coding!
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Khalid wrote:
---------------------- Processing Page --------------  // If you are using Servlet , there are different ways with JSP


You would not do any processing in a JSP page.
That would be a poor design choice.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about a form in a browser?


No, i'm talking about having a standalone java form that I have created and when you put all of your information in it sends it to a local database
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:
Are you talking about a form in a browser?


No, i'm talking about having a standalone java form that I have created and when you put all of your information in it sends it to a local database


Standalone?

is it Swing/AWT ?
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
M Khalid wrote:
---------------------- Processing Page --------------  // If you are using Servlet , there are different ways with JSP


You would not do any processing in a JSP page.
That would be a poor design choice.


No, processing is with Servlet.
I mean to say there was, there are different ways to do processing in JSP aswell.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes swing /AWT
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Khalid wrote:
I mean to say there was, there are different ways to do processing in JSP aswell.


All I was pointing out is that those ways of processing in a JSP would be bad design.
We see a lot of code in the JSP/Servlet forums where people stick code in a JSP page that should be handled by a servlet.  People submitting forms to a JSP, for example.
That would be an incorrect use of a JSP.

Your example was fine, in that a servlet is handling the processing.
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:Yes swing /AWT


OK, using Swing you would have an ActionListener listening on a Save button.

This listener will pull together the relevant data from the form as a model object (see M Khalid's example above) and submit (in a SwingWorker thread) to a DAO (again, see above) which will handle the saving.
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
M Khalid wrote:
I mean to say there was, there are different ways to do processing in JSP aswell.


All I was pointing out is that those ways of processing in a JSP would be bad design.
We see a lot of code in the JSP/Servlet forums where people stick code in a JSP page that should be handled by a servlet.  People submitting forms to a JSP, for example.
That would be an incorrect use of a JSP.

Your example was fine, in that a servlet is handling the processing.


Thanks! , yes exactly using JSP for processing is a bad idea completely.
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick comment on that Swing code.
The call to register (and probably validate) should be done by a Worker, and the actual DB code should be a DAO.
Doing it without the SwingWorker and you run the risk of the GUI locking up while the DB code is executed.

In fact, those ones (IMO) would be in a controller style of object that would be listening on the GUI class the ActionListener sits in.
But that's something that can be done as a later refinement depending on where you are in the learning curve.
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Quick comment on that Swing code.
The call to register (and probably validate) should be done by a Worker, and the actual DB code should be a DAO.
Doing it without the SwingWorker and you run the risk of the GUI locking up while the DB code is executed.

In fact, those ones (IMO) would be in a controller style of object that would be listening on the GUI class the ActionListener sits in.
But that's something that can be done as a later refinement depending on where you are in the learning curve.


Yes. Agreed.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about the code you posted. Lets say I have 1 database but 4 tables to hold different pieces of information does that mean I need 4 different try and catch blocks?

Also another question regarding the following code



User_details being the table? and name, password, email and area is that the column names?


 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing I just remembered is I want to have a radio buttons and let's say that I have 2 radio buttons in my form. Male and female. What would be the process of storing the radio button selected inside the database aswell?
 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first off you ought to use a PreparedStatement.
If you don't understand what that is then you probably need to go through the JDBC tutorial over at Oracle, specifically this bit.

As suggested earlier, you also ought to have a model class that represents the data you want to save.

How you save the radio buttons is up to you.
They could simply be stored a 'M' or 'F'.
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:I have a question about the code you posted. Lets say I have 1 database but 4 tables to hold different pieces of information does that mean I need 4 different try and catch blocks?

Also another question regarding the following code



User_details being the table? and name, password, email and area is that the column names?




It depends, you can update all 4 tables in one try-catch block using different Statement.
Refer my Previous Code w.r.t Web Application, there you can see i have used SELECT and INSERT both in try-catch block.

user_details : Database Table
Name,Password,Email,Address : user_details Table Columns
name, password, email and area : Values from Form

Storing Male or Female can be done by various ways,
Single column with M/F values or Male/Female in DB  and
by process do you mean codes?
 
M Khalid
Ranch Hand
Posts: 50
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Mitchell
you should use PreparedStatement as it is more efficient and useful for DB Queries.
Refer to my previous Post where i used PreparedStatement.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers guys, this should give me plenty of information to work through!
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!