Results 1 to 20 of 27
Thread: Login from database
- 09-19-2011, 02:35 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Login from database
I have created a Desktop application and imported a number of tables and mad them function with the database. but from 1 of the tables i need 4 variables (3 if i can use a for-loop to fill they array's). in order to make a Login screen.
I need username, password and userlevel in the first 3 variables.Java Code:CREATE TABLE werknemer ( userID int(10) NOT NULL auto_increment, username varchar(8) NOT NULL default '', password varchar(255) NOT NULL default '', userlevel tinyint(1) NOT NULL default '0', achternaam varchar(20) NOT NULL default '', voornaam varchar(20) NOT NULL default '', inDienst tinyint(1) NOT NULL default '0', PRIMARY KEY (userID) ) TYPE=MyISAM;
than I will get a username and password from a text field and I need to check if they are allowed to Log in. If the username and password check out the admins variables determins redirect to adminpanel or userpanel. ID is only needed to determain the person that is loged in and is used as return ID[q].
Java Code:private String[] userNames = new String[100]; private String[] passWords = new String[100]; private boolean[] adims = new boolean[100]; private int[] ID = new int[100]; public Login (String loginU,String loginP) { for(int q=0; q <100; q++) { if(userNames[q].equals(loginU) && passWords[q].equals(loginP)) { if(admins[q] == 1) { //.........to admin page no help needed here } else { //.........to user page with ID no help needed here } } } }
Can somebody please help me by tell me either how to fill the array's with the data that is currently in the MqSQL database? or how I can use something like the if in this program while pointing directly at the database?
Database: urenReg
Table: werknemer
collums: userID(int)
username(varchar)
password(varchar)
userlvl(tinyint)
A good tutorial would also be a good help if it also tells me what i need to do this.Last edited by _-Blackhawk-_; 09-19-2011 at 02:43 PM.
- 09-19-2011, 03:15 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
You really don't want to be loading your database info up into your Java app.
Just query the database:
SELECT <whatever fields you want, but not password> FORM werknemer WHERE username = ? and password = ?
This assumes you know JDBC.
- 09-19-2011, 03:36 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
can you please give an example of how to use this in a Desktop application.
because i don't know how to get they information in the app there is a class that is named after each table that does:
@NamedQuery(name = "Werknemer.findByUsername", query = "SELECT w FROM Werknemer w WHERE w.username = :username"),
but I don't have any idea how to get this in to the for-loop.
- 09-19-2011, 03:42 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
Um.
What are you using?
JPA?
- 09-19-2011, 03:49 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
Netbeans 7.0.1 (largest download)
Desktop application (Swing Application Framework)
- 09-19-2011, 04:21 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
@NamedQuery(name = "Werknemer.findByUsername", query = "SELECT w FROM Werknemer w WHERE w.username = :username"),
This.
Is this JPA?
- 09-19-2011, 04:30 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
I don't know what JPA means.
The line is from:
If i can use:Java Code:package urenreg100; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name = "werknemer", catalog = "urenReg", schema = "") @NamedQueries({ @NamedQuery(name = "Werknemer.findAll", query = "SELECT w FROM Werknemer w"), @NamedQuery(name = "Werknemer.findByUserID", query = "SELECT w FROM Werknemer w WHERE w.userID = :userID"), @NamedQuery(name = "Werknemer.findByUsername", query = "SELECT w FROM Werknemer w WHERE w.username = :username"), @NamedQuery(name = "Werknemer.findByPassword", query = "SELECT w FROM Werknemer w WHERE w.password = :password"), @NamedQuery(name = "Werknemer.findByUserlevel", query = "SELECT w FROM Werknemer w WHERE w.userlevel = :userlevel"), @NamedQuery(name = "Werknemer.findByAchternaam", query = "SELECT w FROM Werknemer w WHERE w.achternaam = :achternaam"), @NamedQuery(name = "Werknemer.findByVoornaam", query = "SELECT w FROM Werknemer w WHERE w.voornaam = :voornaam"), @NamedQuery(name = "Werknemer.findByInDienst", query = "SELECT w FROM Werknemer w WHERE w.inDienst = :inDienst")}) public class Werknemer implements Serializable { @Transient private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "userID") private Integer userID; @Basic(optional = false) @Column(name = "username") private String username; @Basic(optional = false) @Column(name = "password") private String password; @Basic(optional = false) @Column(name = "userlevel") private boolean userlevel; @Basic(optional = false) @Column(name = "achternaam") private String achternaam; @Basic(optional = false) @Column(name = "voornaam") private String voornaam; @Basic(optional = false) @Column(name = "inDienst") private boolean inDienst; public Werknemer() { } public Werknemer(Integer userID) { this.userID = userID; } public Werknemer(Integer userID, String username, String password, boolean userlevel, String achternaam, String voornaam, boolean inDienst) { this.userID = userID; this.username = username; this.password = password; this.userlevel = userlevel; this.achternaam = achternaam; this.voornaam = voornaam; this.inDienst = inDienst; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { Integer oldUserID = this.userID; this.userID = userID; changeSupport.firePropertyChange("userID", oldUserID, userID); } public String getUsername() { return username; } public void setUsername(String username) { String oldUsername = this.username; this.username = username; changeSupport.firePropertyChange("username", oldUsername, username); } public String getPassword() { return password; } public void setPassword(String password) { String oldPassword = this.password; this.password = password; changeSupport.firePropertyChange("password", oldPassword, password); } public boolean getUserlevel() { return userlevel; } public void setUserlevel(boolean userlevel) { boolean oldUserlevel = this.userlevel; this.userlevel = userlevel; changeSupport.firePropertyChange("userlevel", oldUserlevel, userlevel); } public String getAchternaam() { return achternaam; } public void setAchternaam(String achternaam) { String oldAchternaam = this.achternaam; this.achternaam = achternaam; changeSupport.firePropertyChange("achternaam", oldAchternaam, achternaam); } public String getVoornaam() { return voornaam; } public void setVoornaam(String voornaam) { String oldVoornaam = this.voornaam; this.voornaam = voornaam; changeSupport.firePropertyChange("voornaam", oldVoornaam, voornaam); } public boolean getInDienst() { return inDienst; } public void setInDienst(boolean inDienst) { boolean oldInDienst = this.inDienst; this.inDienst = inDienst; changeSupport.firePropertyChange("inDienst", oldInDienst, inDienst); } @Override public int hashCode() { int hash = 0; hash += (userID != null ? userID.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Werknemer)) { return false; } Werknemer other = (Werknemer) object; if ((this.userID == null && other.userID != null) || (this.userID != null && !this.userID.equals(other.userID))) { return false; } return true; } @Override public String toString() { return "urenreg100.Werknemer[ userID=" + userID + " ]"; } public void addPropertyChangeListener(PropertyChangeListener listener) { changeSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { changeSupport.removePropertyChangeListener(listener); } }
I could also get going again but somehow I can't get that to work the way I'm used to either.Java Code:public String getUsername() { return username; }
- 09-19-2011, 04:38 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
Is that your code?
That looks like JPA (or Hibernate) annotations.
For logging in it seems to be missing a named query for searching by name and password.
- 09-19-2011, 05:07 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
could you make the query with an examble of how to compare it to a predefined String?
- 09-19-2011, 05:21 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
We're not a code factory.
I have no idea of your system, whether this is JPA or Hibernate or some other set of annotations...nothing.
I have no idea what you're trying to do.
If (as seems likely) this isn't your code then your best bet is to try and explain what you're up to, and why you are using this code to do it.
- 09-20-2011, 11:48 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
As I tryed to explain but will do zo again in more detail:
I am using Java Desktop Application (SWING Application Framework) in Netbeans 7.0.1.
I am using this because it was in almost al the tutorials I read about getting a good connection up and running between Java and MySQL.
I need the information in 4 collums from the werknemer (employee) table in de database urenReg.
I need this information to compare 1 textfield and 1 passwordfield with the usernames and passwords from the table ( if using a password field is a problem i can probably use a normal textfield ) and by comparing those variables i need to determin if it is a viable account.
If yes it should determin if the person that logged in is either a user or and admin by checking userlvl so that they can be send to the correct follow-up screen.
The 4e variable I need is the userID that has to be send along with the useraccounts to let them only edit there own data.
as for the request for an example I wasn't intending to ask you to program everything for me, I generally learn best from examples and testing them and editing somethings in them. if a few lines of code could explain it i would appreciate that but if you know of a good tutorial that explains it than telling me where i can find it would be great to and probably be more helpfull in the long run. I have tryed to find some but every time I get something that looks like: Creating a Custom Java Desktop Database Application - NetBeans 6.7/6.8/6.9 Tutorial
and aldo this has helped me understand and do a lot it doesn't explain how to check a specific table.
as for JPA Hibernate or all the other annotations there are I don't know how to find out what it is but after reading wikipedia on the both of them I think its JPA because of all the MySQL related code.
- 09-20-2011, 12:05 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
So why, if you are learning, are you using code with those annotations in, if you don't understand them?
This is what I am trying to understand.
Learning by using code you have no idea about really won't help you.
Your best bet is to learn JDBC and drop the annotations. Then, when you understand that, you can learn JPA or Hibernate.
In fact, I'm guessing you don't really know SQL either, so you might want to start there?
- 09-20-2011, 12:27 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
If you could point me to a "Good" tutorial for JDBC I will check it out and seriously consider following your advise to learn that first.
as for SQL I only have 1 set database I can work with and I think I understand what is happening there good enough to use it.
- 09-20-2011, 12:29 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
- 09-20-2011, 05:06 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
Oke I can see how it would be a lot easyer to use for me if I can get te connection working first. Its a lot easyer to point to specific tables and collums.
But there are 2 points that I don't understand out the tutorial:
In getting started: 4. where is the " PATH environment variable "? and what is considerd the " executable file "?
I think because of this I can't get past 9.
- 09-20-2011, 05:13 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
Depends what OS you're on.
I'm stuck on Windows 7 so I doubt I'll be able to run you through it.
- 09-21-2011, 08:55 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
OS is Windows XP pro sp3.
well I don't need a run through the entire tutorial I understand most of what happens on first read. but just don't know what those 2 points refer to.
Conciserd doing it in XP mode? has already help me solve a ton of compatibilitie problems!!
- 09-21-2011, 09:25 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Login from database
It's the environment variables in one of the bits of the control panel for your user account.
- 09-21-2011, 09:31 AM #19
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
Thx found it!! now on with the tutorial.
- 09-21-2011, 03:06 PM #20
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Login from database
After a few more pages on the Tutorial I got stuck again. Then I decided try some example I found somewhere that looked like the same think as the tutorial only a whole bunsh of short parts together.
And I got it to work so far that I can decide myself what data i need form the database
(and yes I do understand what the example is doing not simply copy-pasted it).
Thx for the help Tolls
Continued with another problem.Last edited by _-Blackhawk-_; 09-21-2011 at 05:13 PM.
Similar Threads
-
Database for Login system
By Chris_X in forum JDBCReplies: 1Last Post: 11-27-2010, 01:14 AM -
Creating a login form using sql server 2005 database
By kine in forum New To JavaReplies: 7Last Post: 05-29-2010, 01:57 PM -
Login page which connects to database that verifies user
By Neutrino in forum JDBCReplies: 1Last Post: 03-15-2010, 01:39 AM -
login.jsp
By roo7 in forum Enterprise JavaBeans (EJB)Replies: 5Last Post: 12-02-2008, 03:58 PM -
cannot open database requested in login. Login fails
By banduskank in forum JDBCReplies: 0Last Post: 06-25-2008, 12:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks