it's not connected with DB... in this program for banking transaction application

Recommended Answers

All 2 Replies

<%@ page language="java" import="java.util.*,java.sql.*,java.text.*;"%>
<%
  
  String user = "raja";
  String account_no = request.getParameter("accountno");
   String cashType = request.getParameter("cashType");
   int amount = Integer.parseInt(request.getParameter("amount").toString());
   int netamount=0;
  //out.println(user);
  // out.println(account_no);
   //.println(cashType);
   //out.println(amount);
   //out.println(cashdate);
  	   
    try {
         Class.forName("net.sourceforge.jtds.jdbc.Driver");
         Connection conn=DriverManager.getConnection("jdbc:jtds:sqlserver://SERVER:1433/banking","USER","PASSWORD"); 
         Statement st=conn.createStatement();
      
	  String strQuery = "select count(*),netamount from transaction where username='"+user+"' and accountno='"+account_no+"' and id=(select max(id) from transaction) group by id"; 
	  out.println("1" +strQuery);
	  ResultSet rs = st.executeQuery(strQuery);
	  out.println("Welcome");
	   if(rs.next())
		{
		    out.println("in select");
		   if(cashType.equals("Deposit"))
			{
              netamount = amount +  rs.getInt(2);    
			}
			else if(cashType.equals("WithDrawn"))
			{
				if(rs.getInt(2)>amount){
                 netamount = rs.getInt(2)-amount; 
				}
				else
				{
					 response.sendRedirect("cash.jsp?errmsg=error");
				}
			}
			strQuery = "insert into transaction  accountno='"+account_no+"',username='"+user+"',amount='"+amount+"',cashType='"+cashType+"',netamount='"+netamount+"'";
             int i = st.executeUpdate(strQuery);
			 if(i>0)
			{
				  response.sendRedirect("cash.jsp");
			}
			
		}
		else
		{
			netamount = amount;
		  	strQuery = "insert into transaction set accountno='"+account_no+"',username='"+user+"',amount='"+amount+"',cashType='"+cashType+"',netamount='"+netamount+"'";
			out.println("2 "+strQuery);
			 int i = st.executeUpdate(strQuery);
			 if(i>0)
			{
				  response.sendRedirect("cash.jsp");
			}
		}
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
    } catch (Exception e) {
      e.printStackTrace();
    }
 %>

1. DB connection from JSP page = bad practice. Have look at this for better way
2. What is exact error you getting?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.