------ 2 DAYS AGO --------
"I made a database only for the "likes" of a website. The database has a table (Likes) with several fields (Like1, Like2, etc ...)
In a JSP file I made the connection and show the value of a field (Like1).
But what I want to do is: by clicking on a certain text, the value increases by one, updating that value in the database from Like1 to Like1 + 1."
--- EDIT ---
When i click the button, show me this:
When I refresh, the data does not change. So I think if I did not show the error, it would work.
What I want is, when you press the button, the DB data increment by 1.
Structure:
index.jsp:
<body>
<div class="col-lg-12">
<form method="post" action="likeCount">
<button type="submit" name="click"><b style="color:#ff5858;font-size: 24px;">❤</b></button>
<% try {
Connection conn = DBConnect.connect();
PreparedStatement pstmt = conn.prepareStatement("SELECT Like1 from Likes");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {%>
<input type="text" hidden name="counts" value="<%=rs.getInt("Like1")%>" />
</form>
<span style="font-weight: bold"><%=session.getAttribute("count")==null)?rs.getInt("Like1"):(session.getAttribute("count"))%></span><% }
} catch (SQLException ex) {
System.out.println("Error in select: " + ex.getMessage());
}%>
</div>
</body>
DBConnect.java:
package myPack;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect {
public static Connection connect() throws SQLException {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/Likes";
String username = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Error : " + e.getMessage());
}
return conn;
}
}
likeCount.java:
package myPack;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class likeCount {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
/*check whether the button is clicked*/
if (request.getParameter("click") != null) {
try {
/*getting the number of likes*/
String x = request.getParameter("counts");
int c = Integer.parseInt(x);
int count = c;
count = count + 1; //increment the value
Connection conn = DBConnect.connect();
PreparedStatement ps = conn.prepareStatement("UPDATE Likes SET Like1 = ?");
ps.setInt(1, count);
int num = ps.executeUpdate();
if (num > 0) {
HttpSession session = request.getSession();
session.setAttribute("count", count);
response.setIntHeader("Refresh", 3);
response.sendRedirect("index.jsp");
}
} catch (SQLException e) {
System.out.println("Error in update :" + e.getMessage());
}
}
}
}