Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

pls suggest me a good style of programming

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class issueServlet extends HttpServlet {
Connection con,con1,con2,con3,con4,con5;
Statement st,st1;
PreparedStatement pt,pt1,pt2,pt3,pt4;
ResultSet result,result1,result2;

/**
* Constructor of the object.
*/
public issueServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String action=request.getParameter("B1");
System.out.println("the value of action is "+action);
//System.out.println(action.equals("Submit"));
//System.out.println(action=="Submit");

String issued[]=request.getParameterValues("status");
Vector vect=new Vector();
for(int i=0;i<issued.length;i++)
vect.addElement(issued[i]);
HttpSession session=request.getSession();
session.setAttribute("issued",vect);
if(action.equals("Submit"))
{
for(int i=0;i<issued.length;i++)
{
System.out.println("hi");
String issue=issued[i];
pt=con.prepareStatement("select * from Request2 where EmpName=?");
pt.setString(1,issue);
result=pt.executeQuery();
System.out.println("succesffully executed befor if from issue servlet");
//pt.close();
if(result.next())
{
//pt.close();
System.out.println("befor updateion from issue servlet");
pt1=con1.prepareStatement("insert into Issued values(?,?,?,getdate())");
pt1.setString(1,result.getString(1));
String itemName=result.getString(2);
pt1.setString(2,itemName);
int p=result.getInt(3);
pt1.setInt(3,p);
//pt1.setDate(4,getdate());
int k=pt1.executeUpdate();
System.out.println("the updated variable is "+k);
if(k>0)
{
pt2=con2.prepareStatement("delete from Request2 where EmpName=?");
pt2.setString(1,issue);
pt2.executeUpdate();
System.out.println("successfully deletted from Request2 table");
pt3=con3.prepareStatement("select AvailQuantity from InventoryReg where ItemName=?");
//pt.setInt(1,p);
pt3.setString(1,itemName);
result1=pt3.executeQuery();
result1.next();
int avail=result1.getInt(1);
int remain=avail-p;
System.out.println("just vefore");
if(remain<0)
remain=0;
System.out.println("just vefore");

pt4=con4.prepareStatement("update InventoryReg set AvailQuantity=? where ItemName=?");
System.out.println("just ftere");
pt4.setInt(1,remain);
pt4.setString(2,itemName);
pt4.executeUpdate();
System.out.println("successfully updated the avilcount");
response.sendRedirect("http://localhost:8081/EasyManager/adminResponse.jsp");
//int avail=result1.getInt(1);

}
}
pt.close();
//con1.close();
pt1.close();
pt2.close();
pt3.close();
pt4.close();
//con.close();
//result.close();
//out.println("the issued values are "+issued[i]);
//System.out.println("the issued values are "+issued[i]);
out.flush();
out.close();
}
}
else
{
for(int i=0;i<issued.length;i++)
{
System.out.println("hi");
String issue=issued[i];
pt1=con1.prepareStatement("select * from Request2 where EmpName=?");
pt1.setString(1,issue);
result=pt1.executeQuery();
result.next();
pt2=con2.prepareStatement("insert into Invalid values(?,?,?,getdate())");
pt2.setString(1,result.getString(1));
pt2.setString(2,result.getString(2));
pt2.setInt(3,result.getInt(3));
pt2.executeUpdate();
System.out.println("the items are inseted into delete Table");
pt=con.prepareStatement("delete from Request2 where EmpName=?");
pt.setString(1,issue);
int a=pt.executeUpdate();
System.out.println("succesffully deleted form Request2 ");
response.sendRedirect("http://localhost:8081/EasyManager/deleteResponse.jsp");
pt.close();
pt1.close();
pt2.close();
}
}

}catch(Exception e){System.out.println("form catch of issueServlet "+e);}
}

public void init() throws ServletException {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:invent","sa","");
con1=DriverManager.getConnection("jdbc dbc:invent","sa","");
con2=DriverManager.getConnection("jdbc dbc:invent","sa","");
con3=DriverManager.getConnection("jdbc dbc:invent","sa","");
con4=DriverManager.getConnection("jdbc dbc:invent","sa","");
con5=DriverManager.getConnection("jdbc dbc:invent","sa","");
}catch(Exception e){System.out.println("form init "+e);}
// Put your code here
}

}


This is the servlet code that i am using in my applicatoin.here i had created many statement objects,connection objects and alos more than one result set.and if i avoid using this much objects i am getting an exception like"connection is busy with some other hstmt".what i believe is teher may be some other way to arrange the connection ,satements,and resultSet objects.can anyone help me regardig this.(pls tell me in which way i have to rearrange the code to avoid the exception and in a good programming style

thanks in advance
susanth
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, "pls" is not a word. Please use real words.
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 07, 2006: Message edited by: Lynette Dawson ]
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Few tips:

1. Think twice before using instance variables in your servlet. Just read upon any servlet tutorial to understand why this is BAD.


Connection con,con1,con2,con3,con4,con5;
Statement st,st1;
PreparedStatement pt,pt1,pt2,pt3,pt4;
ResultSet result,result1,result2;



2. You do not need a new connection for every statement you execute.

3. Use Log4J or Apache Commons Logging instead of System.out.println()

4. You have cllaed the request parameter "status" and you assign it to a variable called "issued" which corresponds to a database column called "EmpId"... Please use meaningful names

5. I have replaced your loop with a single SQL statement,


String sql = "INSERT INTO Issued (col1,col2,col3,datecolumn)"
+ " SELECT blah1,blah2,blah3,getDate() "
+ " FROM Request "
+ " WHERE EmpName IN ("
+ empNames
+ ")";



6. Always do

if ("Submit".equals(action))


instead of

if (action.equals("Submit"))



The former will not throw a null pointer exception if action is null, whereas the later will
 
reply
    Bookmark Topic Watch Topic
  • New Topic