Jump to content

login update problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
good day everybody, i want to used this code for users login but each time i want the count column to update, it will return an empty page. I know may be i did not structure
this code properly especially

String query1="UPDATE vata SET count = count + 1 WHERE username='"+username+"'";

 int i = stmt.executeUpdate(query1);


below is the main code



<%@ page import="java.sql.*" %> 

<%@ page import="java.io.*" %> 

<html> 

<head> 

    <title>login</title>

</head> 

<body>

<%  

response.setContentType("text/html");

PrintWriter pw = response.getWriter();

String username = request.getParameter("username");

String password = request.getParameter("password");

int count=0;


try{




 Class.forName("com.mysql.jdbc.Driver").newInstance();



Connection con=null;

ResultSet rst=null;

Statement stmt=null;



con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vat", "root", "root");




stmt=con.createStatement();

String query = "select count from vat where username='"+username+"' and password='"+password+"'";

System.out.println(query);

ResultSet rs = stmt.executeQuery(query);

while(rs.next()){

//count=rs.getInt(1);

}

if(count > 1){

pw.println("<font color=green size=5>you cannot login again</font>");

}

else{

String query1="UPDATE vata SET count = count + 1 WHERE username='"+username+"'";

 int i = stmt.executeUpdate(query1);		

pw.println("<font color=red size=5>welcome friend</font>");

}

}


catch(Exception e){

System.out.println(e.getMessage());

}



%> 



thank u



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Are you sure the table name is "vata", as in the first query you use "vat"?

#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
the table is not the problem. I think the problem is how to perform both insert and update simultaneously.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

mutago said:

the table is not the problem. I think the problem is how to perform both insert and update simultaneously.
What insert?

I don't think there's anything wrong with the code.
Why is //count=rs.getInt(1); commented out, if a column named "count" exists in the vat table, then it should work just fine.
If it doesn't exist, then I think you need

SELECT COUNT(*) 

FROM vat 

WHERE username= 'wim' and password='DC'

which leads to this string for your query:

"select count(*) from vat where username='"+username+"' and password='"+password+"'"






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users