total = amount + rs.getInt(1);for example amount = first or existing amount in the database=300
rs.getInt(1) = second amount to be inputed =200
total =500.
Now when I want to input the third amount as per rs.getInt eg 400
the total amount will still remain 500 instead of 900 that is 300+200+900
My question is how do solve the problem so that the total column will be on the increase whenever new amount as per rs.getInt(1) is added
amount = first table column and data type is int hence getInt(1)
total = second column
student= third column
below is the code
<%@ page language="java" import="java.util.*,java.sql.*,java.text.*;"%>
int amount = Integer.parseInt(request.getParameter("amount").toString());
int total=0;
String studentname = request.getParameter("studentname");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement st = conn.createStatement();
String strQuery = "select * from student";
ResultSet rs = st.executeQuery(strQuery);
if(rs.next())
{
out.println("in select");
if(studentname.equals(""))
{
total = amount + rs.getInt(1);
}
}
strQuery = "insert into student set amount='"+amount+"',total='"+total+"',studentname='"+studentname+"'";
int i = st.executeUpdate(strQuery);
if(i>0)
{
response.sendRedirect("success.jsp");
}
}
else
{
amount = amount;
strQuery = "insert into student set amount='"+amount+"',total='"+total+"',studentname='"+studentname+"'";
out.println("2 "+strQuery);
int i = st.executeUpdate(strQuery);
if(i>0)
{
response.sendRedirect("success.jsp");
}
}
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
%>
Edited by ZekeDragon, 22 February 2011 - 05:10 PM.
Code = Java does *not* work in the [code] tags.


Sign In
Create Account


Back to top









