I'm getting the error in the first line i guess! Please help me out in dealing with it. I'm basically trying to execute a query through my front end
try {
ResultSet rs=st.executeQuery("select *from Login Username ='"+username+"' and Password '"+password+"'");
rs.last();
int counter = rs.getRow();
if (counter==1){
JOptionPane.showMessageDialog(null,"Username and Password Correct","Username and Password Correct",JOptionPane.INFORMATION_MESSAGE);
this.setVisible(false);
new Menu().setVisible(true);
hide();
}else{
jt_username.setText("");
jp_password.setText("");
JOptionPane.showMessageDialog(null,"Error","Username and Password Incorrect",JOptionPane.INFORMATION_MESSAGE);
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
Your SQL is missing the WHERE keyword. Try this on for size:
ResultSet rs=st.executeQuery("select * from Login WHERE Username ='"+username+"' and Password= '"+password+"'");
Related
[enter image description here][1]while installing mysql on pc, i got an error message that "port 3306 is currently in use. provide another port"
so i changed the port number to 3305.
now i am trying to connect java project in netbeans ide to mysql, but it ain't working even though i tried with both the port numbers.
enter code here
String pword= password.getText();
String name= tf.getText();
String str= null;
Statement stmt= null;
ResultSet rs= null;
Connection conn=null;
try{
Class.forName("java.sql.Driver");
String pwd= "mysql";
String uid="root";
String url="jdbc:mysql://localhost:3306/project";
try{
conn= (Connection)DriverManager.getConnection(url,uid,pwd);
stmt= conn.createStatement();
String sql= "SELECT * FROM login WHERE name='" + name+ "'";
rs= stmt.executeQuery(sql);
rs.next();
str= rs.getString("password");
if(str.equals(pword))
{
menu m= new menu();
m.setVisible(true);
this.setVisible(false);
}
else
JOptionPane.showMessageDialog(null, "Incorrect username or password!");
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Incorrect username or password!");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error in Connectivity");
}
I did something like this (for MS SQL server) recently however I don't think you specified the database name
`String url ="jdbc:sqlserver://YourPCname\\instancename;databaseName=yourDBname";`
(note you'd need to change "sqlserver" to "mysql")
Also make sure that your server is running.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sql="select * from account where Username and Password=(?,?)"; //I think here is some error but dont know what it is
try
{
stm=con.prepareStatement(sql);
stm.setString(1,jTextField1.getText());
stm.setString(2,jPasswordField1.getText());
rs=stm.executeQuery();
if(rs.next())
{
rs.close();
stm.close();
setVisible(false);
Loading ob =new Loading();
ob.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect username and password");
}
}
catch(SQLException exp)
{
JOptionPane.showMessageDialog(null, exp);
}
finally
{
try
{
rs.close();
stm.close();
}
catch(SQLException exp)
{
exp.printStackTrace();
}
}
}
String sql="select * from account where Username and Password=(?,?)";
Well I'm no SQL expert but I think it should be:
String sql="select * from account where Username = ? and Password=?";
I hope you plan on asking a question every time you have a little problem. You can't just guess what the SQL syntax is, so you need to buy a book or find a tutorial.
Maybe the JDBC Database Access can give you some general information.
You want
WHERE username=? AND password=?
Use below query:
"select * from account where Username=? and Password=?;"
You should use simple Statement instead of PreparedStatement, and put the user and pass field text in your query.
This sample code may help you
stmt = con.createStatement();
String sql = "select * from table where Username='"+user.getText()+"' AND Password='"+pass.getText()+"'";
res = stmt.executeQeury(sql);
Im currently attempting to make a login system in Java and SQL and am currently getting an error.
PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?");
s1.setString(1,loginUsername);
s1.setString(2, loginPassword.toString());
ResultSet rs = s1.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "User Found");
}
else{
JOptionPane.showMessageDialog(null, "Error");
}
When I run that snippet of code it always displays the else part of the if/else and im not sure why. Thank in advance.
Edit: Yes the DB is populated with users.
Edit2: I changed the function to require two string args.
You can fetch number of rows available in your table for given query and if it is great than 0 it will execute User Found statement.
PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?");
s1.setString(1,loginUsername);
s1.setString(2, loginPassword.toString());
ResultSet rs = s1.executeQuery();
int rowCount = rs.last() ? rs.getRow() : 0; //Number of rows available in table for query
if(rowCount>0)
JOptionPane.showMessageDialog(null, "User Found");
}
else{
JOptionPane.showMessageDialog(null, "Error");
}
You must try to use while(rs.next()) instead of if(rs.next()).
If that does not work, does your DB (which you have mentioned is populated), contain those particular values of username and password that you are searching for ? If not, that may be the problem.
If that too does not work, you may have closed some objects (eg. s1.close() or con.close() ) where you shouldn't. For further help on that you will need to provide a more detailed flow of what you have done in the whole code.
public void Login(String user, String pass) throws ClassNotFoundException, SQLException{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
MainWindow mw = new MainWindow();
String userName = "<USERNAME>";
String password = "<PASSWORD>";
String url = "<URL>";
Connection con = DriverManager.getConnection(url, userName, password);
PreparedStatement s1 = con.prepareStatement("SELECT * FROM dbo.Logins WHERE Username=? AND Password=?");
s1.setString(1,user);
s1.setString(2, pass);
ResultSet rs = s1.executeQuery();
//int rowCount = rs.last() ? rs.getRow() : 0; //Number of rows
System.out.println("Name: " + user + "\nPass: " + pass);
if(rs.next())
JOptionPane.showMessageDialog(null, "User Found");
else{
JOptionPane.showMessageDialog(null, "Error");
}
con.close();
I have a table:
create table User1(Username varchar2(20) ,
First_Name varchar2(20) ,
Last_Name varchar2(20) ,
Password varchar2(20) ,
Date_Of_Birth Date )
following jdbc code::
if(e.getSource()==submit)
{
Connection con=null;
ResultSet rs=null;
PreparedStatement st=null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1522:xe", "hr", "hr");
st=con.prepareStatement("select count(1) from User1 where Username = ? and Password = ? ");
//st.setParameter(1, text1.getText());
//st.setParameter(2, p1.getText());
st.setString(1,text1.getText());
st.setString(2,text1.getText());
rs= st.executeQuery();
//String t1=text1.getText();
//String t2= p1.getText();
while(rs.next())
{
if (rs.getInt(1) == 1)
{
new Rec_options();
}
}
}
catch(SQLException ee)
{
System.out.println(ee);
}
catch(Exception e1)
{
System.out.println(e1);
}
finally
{
try
{
rs.close();
con.close();
st.close();
}
catch(SQLException eee)
{
System.out.println(eee);
}
}
}
Is there a problem in while loop ? cause after entering username and password when i click on submit button ,another page "Rec_options" should get opened,but here nothing's happening nor am i getting any error or something in cmd.I'm entering the correct username and password.
Please help!!
Thanks..
You've used "st.setString(1,text1.getText());" twice. I think you should change second one to password string.
You seem to have a typo in your construction of the prepared statement:
st.setString(1,text1.getText());
st.setString(2,text1.getText()); // <---- You are passing your username twice.
Try to use variable name that indicate its type and purpose
txtuser
txtpassword
I am trying to verify username and password with MySQL. But it's not working. I can't find the problem. Can anybody help me fix it?
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String user = jTextField1.getText();
char[] pass = jPasswordField1.getPassword();
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();
String sql = "Select * from tbl_User Where username='" + user + "' and password='"+pass+"'";
rs = stat.executeQuery(sql);
while (rs.next())
{
if (user.equals(rs.getString("username")))
{
if (pass.equals(rs.getString("password")))
{
JOptionPane.showMessageDialog(null,"Login Successfully");
main.getWindows();
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Password");
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Login");
}
}
stat.close();
con.close();
}
catch (SQLException | HeadlessException e)
{
//e.printStackTrace();
JOptionPane.showMessageDialog(null,"PROBLEM OCCURED !!!! ");
}
catch (ClassNotFoundException ex) {
Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex);
}
// TODO add your handling code here:
}
Actually I think it is not checking the enteries with username and password in database. am I right?
Firstly, select by username, then hash the user entered password en check if it matches the hashed password in the database. I suggest something like SHA-2
I also suggest you write classes to handle your code, i.e a User class..
You also forgot to close your ResultSet
One more thing, use PreparedStatement
You are checking for password and username match 2 times.
String sql = "Select * from tbl_User Where username='" + user + "' and password='"+pass+"'";
There you already check the password and user, First you shuld check if the password its not stored as MD5 or any other hash type
After that sql you only need to check if its returns any row like #Prabhakaran says
Check code which is written is connecting to database, password is not there in the below code
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Second is check the user and pass variable is getting the value from the action event.
Do like this
Statement stat = con.createStatement();
user = user.toLowerCase();
pass = pass.toLowerCase();
String sql = "Select * from tbl_User Where LOWER(username)='" + user + "' and LOWER(password)='"+pass+"'";
rs = stat.executeQuery(sql);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Login Successfully");
main.getWindows();
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Login");
}
First things first.
Code will only be used to validate the error. So you must paste the error fired by your program.
Since we don't have enough information to the problem, I will try to help you out.
1- It seems your connection variable missing the "Connection" try this :
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"DATABASENAME"?useTimezone=true&serverTimezone=UTC","USERNAME","PASSWORD");
2 - You already made the if statement to the query in the beginning, so you don't have to start all over again with You can simply type :
if (rs.next()) {
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Password");
} then carry on with the exception part
this is the code :
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();
String sql = "Select * from tbl_User Where username='" + user + "' and password='"+pass+"'";
rs = stat.executeQuery(sql);
if (rs.next())
{
JOptionPane.showMessageDialog(null,"Login Successfully");
main.getWindows();
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Password");
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Login");
}
stat.close();
con.close();
}
catch (SQLException | HeadlessException e)
{
//e.printStackTrace();
JOptionPane.showMessageDialog(null,"PROBLEM OCCURED !!!! ");
}
catch (ClassNotFoundException ex) {
Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex);
}
// TODO add your handling code here:
}`