Login verification from sqlite not working correctly - netbeans-8

Hi everyone I was told by a friend that if I struggle I should go on this website for some guidance. I am a casual after hours programmer and I just do it to enrich my mind. I have a problem at the moment where I want my program to have a login screen and use my sqlite database to verify the username, password and division (administrator, admin, user) before you get to log into the main menu of the program. Now I use Netbeans to do my java programming as it makes it way easier for me and so far it works for the first password and administrator "division" but as soon as I add someone as let say admin or a user nothing happens and I have tried everything I could read up on on the internet as I said I am home schooled when it comes to programming as it is just a hobby and a interest of mine, any advise or a pointer to a website where I can teach myself how to fix my problem will help me.Here is my current code I hope I am posting this correctly:
`private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql ="select Id,Username,Password,Division from Users where (username =? and password= ? and division=?)";
try{
int count = 0;
pst = conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());
pst.setString(3, txt_combo.getSelectedItem().toString());
rs = pst.executeQuery();
while (rs.next()){
int Username =rs.getInt(1);
Emp.empnum = Username;
String username = rs.getString("username");
Emp.empname = username;
count = count+1;
}
String access = (txt_combo.getSelectedItem().toString());
if(access =="Administrator,Admin,User"){
if(count == 1){
JOptionPane.showMessageDialog(null, "Success");
MainMenu j = new MainMenu();
j.setVisible(true);
this.dispose();
}else {
JOptionPane.showMessageDialog(null, "The Username or Password you entered was incorrect!");
}
}
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
pst.close();
}catch (Exception e){
}
}
}`

IU found the function Boolean here is how my code worked perfectly after using it if anyone can see anyway I can better it any advise will do but it works 100%. 'String sql ="select Id,Username,Password,Division from Users where (username =? and password= ? and division=?)";
try{
int count = 0;
boolean foundUser = false;
pst = conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());
pst.setString(3, txt_combo.getSelectedItem().toString());
rs = pst.executeQuery();
while (rs.next()){
int Username =rs.getInt(1);
Emp.empnum = Username;
String username = rs.getString("username");
Emp.empname = username;
foundUser = true;
count = count+1;
}
String access = (txt_combo.getSelectedItem().toString());
if (foundUser) {
JOptionPane.showMessageDialog(null, "Success");
MainMenu j = new MainMenu();
j.setVisible(true);
this.dispose();
}else {
JOptionPane.showMessageDialog(null, "The Username or Password you entered was incorrect!");
}
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
pst.close();
}catch (Exception e){
}
}'

Related

SQL query isn't working when embedded in java

I'm trying to connect the SQL plus database with java but it's not working , I'm using netbeabs ide 8.0.2
Here i have pasted following cod
e in my submit button but when I execute it then it goes for the JOptionPane which is in else part whereas it should go for the if part. I'm working on it but I've got no solution can u help please
String sql ="select * from ADMINTABLE where APOST = ? and USERNAME = ? and PASSWORD = ?";
try {
pst = con.prepareStatement(sql)
pst.setString(1, jComboBox1.getSelectedItem().toString());
pst.setString(2,jTextField1.getText());
pst.setString(3, Arrays.toString(jPasswordField1.getPassword()));
rs = pst.executeQuery();
if (rs.next()) {
rs.close();
pst.close();
con.close();
setVisible(false);
Admin ob = new Admin();
ob.setVisible(true);
} else
JOptionPane.showMessageDialog(null,"username and password not matched Please Enter Again !!!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e);
}

my javafx project doesn't read data from the database

So i created a database table in intellij and i made a javafx project and it's connected to my server(localhost)
now i'm getting errors here
String query = "SELECT * FROM IntellijDB.dbo.Employee WHERE username = ? and passcode = ?";
it says I'm unable to access the table
this is the method
public boolean isLogin(String user,String pass) throws SQLException
{
PreparedStatement preparedStatement = null ;
ResultSet resultSet = null;
String query = "SELECT * FROM IntellijDB.dbo.Employee WHERE username = ? and passcode = ?";
try
{
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1,user);
preparedStatement.setString(2,pass);
resultSet = preparedStatement.executeQuery();
if (resultSet.next())
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
//TODO: handle exception
}
finally {
preparedStatement.close();
resultSet.close();
}
}
and the other error here
if (loginModel.isLogin(userAction.getText(), passAction.getText()))
and this line too
resultSet.close();
and I guess they are related and i don't know how to fix this
this is the other code
#FXML
public void Login(ActionEvent event) {
try {
if (loginModel.isLogin(userAction.getText(), passAction.getText())) {
isConnected.setText("Username & password is correct");
}
else
{
isConnected.setText("Check your username or password");
}
} catch (SQLException e)
{
isConnected.setText("Check your username or password");
//TODO Auto-generated catch block
e.printStackTrace();
}
}

how am i supposed to pull a record from DAO vector?

I'm trying to implement the login page based on Oracle sql using JFrame(swing). I already have inserted IDs and PWs on database. Plus, I've also already defined appropriate -I think- LoginMember method in DAO. Below are the codes for Jframe page and DAO. Please let me know what is wrong with this. I've been struggling with this for the last 5 hours and still have no idea what to do.. HELP!! Sorry if you feel that I have not given enough info to solve this problem.
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strID=tfID.getText();
String strPW=String.valueOf(pwfPW.getPassword());
if(tfID.getText().equals("")) {
JOptionPane.showMessageDialog(Login.this, "Please type your ID");
} else {
dao=new M_DAO();
dao.loginMember(strID,strPW);
if(tfID.getText().equals(dao.loginMember(strID,strPW))) {
JOptionPane.showMessageDialog(Login.this, strID+" : successfully logged in");
}else if(strID ==null && strID.equals(strPW)){
JOptionPane.showMessageDialog(Login.this, "Incorrect ID or PW.");
}else{
System.out.println(10);
}
}
}
});
DAO:
public Vector loginMember(String strID,String strPW) {
Vector items=new Vector();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn=DB.hrConn();
String sql="select * from member where strID=? and strPW=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "strID");
pstmt.setString(2, "strPW");
rs=pstmt.executeQuery();
if(rs.next()){
Vector row=new Vector();
row.add("strID");
row.add("strPW");
items.add(row);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs!=null) rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(pstmt!=null) pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return items;
}
I wish it would work so bad...
It only shows the correct answer when I type nothing on ID and PW textfields, saying "Please type your ID". It prints out 10 when I type something on those.(just to check which line has an error.) Oh, please let me know if I need to specify something else!

authentication in liferay without login hook

I have a problem, I get the user and password of a view and I check if this data is correct in the data of liferay, when it's correct my method return 1 if the validation is true, but I don't know how to make the successful login in liferay, this is my method:
try {
long companyId = PortalUtil.getDefaultCompanyId();
System.out.println(companyId + " id company");
User user1;
try {
user1 = UserLocalServiceUtil.getUserByEmailAddress(companyId, name);
long cmp = user1.getCompanyId();
Company company = CompanyLocalServiceUtil.getCompany(cmp);
int a = UserLocalServiceUtil.authenticateByUserId(company.getCompanyId(), user.getId(), pass, null,
null, null);
if (a == 1) {
System.out.println("Log in successful");
}
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("algo salio mal");
}
This seems to be a case where you would need an auto-login hook. In Liferay 7, you just need components like in: https://www.e-systems.tech/blog/-/blogs/autologin-in-liferay-7
You can use an indicator within the user session, like a token, and check it in a custom logic:
#Override
protected String[] doLogin(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
final long companyId = portal.getCompanyId(request);
final HttpSession session = request.getSession();
// code your logic here..
final String[] credentials = new String[3];
credentials[0] = String.valueOf(user.getUserId());
credentials[1] = user.getPassword();
credentials[2] = Boolean.FALSE.toString();
return credentials;
}
This solution is also valid for LR6, the difference is that you are not using OSGi there, so you have to create a hook through the SDK.

How to get user along with it's computer name from active directory

I am new to using Active Directory and want to create search form which take username from user, do wildcard search using that and returns username found along with it's computer name. I am using following code:
private SearchResultCollection GetSearchResultCollection(string DomainName, string Filter)
{
try
{
/* Getting connection info. */
Login ObjLogin = new Login();
string ServerIP = ObjLogin.GetSystemSettingByName("LDAPServer");
string Port = ObjLogin.GetSystemSettingByName("Port");
string UserName = ObjLogin.GetSystemSettingByName("ADUsername");
string Password = ObjLogin.GetSystemSettingByName("ADPassword");
DirectoryEntry Ldap = new DirectoryEntry(#"LDAP://" + ServerIP + ":" + Port, UserName, Password);
DirectorySearcher LdapSearcher = new DirectorySearcher(Ldap);
LdapSearcher.Filter = Filter;
return LdapSearcher.FindAll();
}
catch (Exception ex)
{
return null;
}
}
public string FindUsernameInAD(string Username, out int Count)
{
Count = 0;
string FoundUsername = Username;
try
{
/* Getting domain name */
Login ObjLogin = new Login();
string DomainName = ObjLogin.GetSystemSettingByName("DomainName");
SearchResultCollection LdapSearcherResults = GetSearchResultCollection(DomainName, "(&(objectClass=user))");
foreach (SearchResult resultLdap in LdapSearcherResults)
{
if ((resultLdap.Properties["sAMAccountName"] != null) && (resultLdap.Properties["sAMAccountName"].Count > 0))
{
if (resultLdap.Properties["sAMAccountName"][0].ToString().ToLower().Contains(Username.ToLower()))
{
//return resultLdap.Properties["cn"][0].ToString() + "(" + resultLdap.Properties["sAMAccountName"][0].ToString() + "#" + Username + ")";
FoundUsername = resultLdap.Properties["sAMAccountName"][0].ToString();
Count++;
}
}
}
}
catch (Exception ex)
{
/* Catch Exception Here */
log.Error("Error in UserManager.cs, FindUsernameInAD function: " + ex.Message);
}
return FoundUsername;
}
This code returns me username but no computer name so how do I get computer name? Also I want to know that "Is computer name for user is unique for each user in active directory?"