Results 1 to 7 of 7
- 05-25-2010, 07:38 AM #1
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
comparing username and password fields for equality.
Am new to java.How can I solve this small problem? Am using database based authentication to enable mi logon.Am trying to compare the username and passfield fields for equality but I dont know the best conditional operator/logic operator to use. Here is my sample code that contains a red wavy underline in the Netbeans IDE am using.
If (userName !false) && (password !true)
System.out.println("Invalid Password")
Please help me out and show me the best way to do it?
- 05-25-2010, 07:40 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The first thing is that you have to be able to know what you want to do and be able to describe it.
Say in words what you want to achieve. Start with "if".
- 05-25-2010, 08:04 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
In Boolean logic 'not false' is true and 'not true' is false, so you can simplify that expression a bit:
But what does that mean? You have a variable next to a boolean value; that doesn't make sense in Java; it is the same as, say:Java Code:if (username true) && (password false)
That doesn't make sense either. First try to put in words what you have in mind and then try to translate it to Java.Java Code:count 42
kind regards,
Jos
- 05-25-2010, 09:59 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Um, unless I'm missing something here, since your user/password data is in a db why aren't you doing this in the SELECT statement?
There should be no need whatsoever to get the password out of the db and then do a comparison.
- 05-25-2010, 10:34 AM #5
in order to compare a password and a username in your java-code you must hold the password and the username in your code and this is a bad idea for security reasons. usually you let enter the user his account name and the password and send it to the database for validation. so if the authentication was successful you will get an connection otherwise an exception. this means invalid account name and/or invalid password.
- 05-25-2010, 02:13 PM #6
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
comparing username and password fields for equality
This is my sample code.Alot is missing j2me64 but I think U can edit it out and send me a reply and please show me how i can only let the user insert there username for them to login.Am new to java and thanks for your assistance.
package Login_form;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class Login extends javax.swing.JFrame {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
int curRow = 0;
private String password;
public Login() {
initComponents();
DoConnect();
}
public void DoConnect () {
try {
String driver = "org.apache.derby.jdbc.Client";
String url="jdbc:derby://localhost:1527/sample?user=<userName>&password=<Password>";
String uName="app";
String uPass="app";
stmt=con.createStatement();
String sql="INSERT INTO APP.LOGIN values (USERNAME, PASSWORD)";
rs=stmt.executeQuery(sql);
if (rs.next()) {
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
btnUserName = new javax.swing.JTextField();
btnPassword = new javax.swing.JPasswordField();
btnLogin = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("UNRA-LABOUR BASED ROAD WORKS MANAGEMENT SYSTEM");
jLabel1.setText("USERNAME");
jLabel2.setText("PASSWORD");
btnLogin.setText("LOGIN");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnLogin)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnPassword)
.addComponent(btnUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))))
.addContainerGap(40, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btnLogin)
.addContainerGap(22, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(87, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(98, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
String username = "Username";
String Password = "Password";
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnLogin;
private javax.swing.JPasswordField btnPassword;
private javax.swing.JTextField btnUserName;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
- 05-25-2010, 02:24 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
1. Use code tags when posting code, otherwise you get unformatted code like yours is, which is almost impossible to read.
2. Split out the display from the db access. Your current code is a huge blob of stuff that makes it difficult to follow what's going on, and will be hard to debug.
3. You are doing an INSERT statement and then treating it as a query. INSERTs do not give you a result set.
Similar Threads
-
password username and databases
By chalo in forum JCreatorReplies: 0Last Post: 12-02-2008, 08:11 AM -
username password verification
By bheezee in forum JDBCReplies: 0Last Post: 11-25-2008, 06:55 PM -
JSTL -- Comparing two strings for equality
By trinkets in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-12-2008, 04:39 PM -
Help, created a username and password box
By cachi in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:21 AM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks