Results 1 to 7 of 7
  1. #1
    arunkumarinfo is offline Member
    Join Date
    Jan 2009
    Posts
    15
    Rep Power
    0

    Exclamation how to minimize the code.

    import java.io.*;
    import java.sql.*;
    import java.sql.Statement.*;
    public class work
    {
    public static void main(String args[])throws SQLException
    {

    String str=null,str1=null,stt=null,str2=null;
    Connection con =null;
    ResultSet rs =null;
    Statement st =null;
    String status=null,custid[];
    int count=0,count1=0,count2=0,rowcount=0;
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:tiens") ;
    }
    catch(Exception e)
    {
    System.out.println("CONNECTION ERROR!!");
    }
    st=con.createStatement();
    str1="select count(*) from customer";
    rs =st.executeQuery(str1);
    while(rs.next())
    {
    rowcount=rs.getInt(1);
    }
    custid=new String[rowcount+1];
    str2="select customerid from customer";
    rs=st.executeQuery(str2);
    int j=0;
    while(rs.next())
    {
    custid[j++]=rs.getString("customerid");
    }
    for(int k=0;k<=2;k++)
    {
    for(int z=0;z<=rowcount;z++)
    {
    str ="select count(*) from customer where sponsorid='"+ custid[z] +"' ";
    rs =st.executeQuery(str);
    while(rs.next())
    {
    count=rs.getInt(1);
    }
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 2 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count1=rs.getInt(1);
    }
    try
    {
    if(count1>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 3 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }catch(Exception e)
    {
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    }
    try
    {
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 3 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count2=rs.getInt(1);
    }
    if(count2>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 4 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }
    }catch(Exception e)
    {
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    try
    {
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 4 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count2=rs.getInt(1);
    }
    if(count2>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 5 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }
    }catch(Exception e)
    {
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    try
    {
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 5 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count2=rs.getInt(1);
    }
    if(count2>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 6 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }
    }catch(Exception e)
    {
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    try
    {
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 6 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count2=rs.getInt(1);
    }
    if(count2>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 7 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }
    }catch(Exception e)
    {
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    try
    {
    if(count>=3)
    {
    st=con.createStatement();
    str1="select count(*) from customer where sponsorid='"+ custid[z] +"' and status>='"+ 7 +"' ";
    rs=st.executeQuery(str1);
    while(rs.next())
    {
    count2=rs.getInt(1);
    }
    if(count2>=3)
    {
    st=con.createStatement();
    stt = "update customer set status= '" + 8 + "' where customerid = '"+ custid[z] +"'";
    st.executeUpdate(stt);
    st.execute("commit");
    rs.close();
    st.close();
    }
    }
    }catch(Exception e){
    System.out.println("SQL EXCEPTION ERROR !!!");
    }
    }
    }
    }
    }

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Why are you posting the same question twice?
    Pick a forum and post once.

    You waste our time looking at it twice, trying to see if they are different questions.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    it would be nice if you would at least identify where you think you can slim your code down.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Also, please ident your code, and then use CODE tags (square braces with CODE, /CODE inside) to make your code readable.

    Second, writing an entire program in the main() method reminds of poor code I worked on in 1990. Set up a proper class(es), and use methods to contain each action the class performs.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    aboylearning is offline Member
    Join Date
    Jan 2009
    Posts
    16
    Rep Power
    0

    Default

    Hi all, believe we should not criticize this block and get to the business.

    Hi arunkumarinfo, from your subject it look like you wanted to make your code look more slimmer/shorter but can you please also post from where exactly you wanted here people to see? And please explain what this code is doing? And also please write the code in a way that it is easy to read. Looking forward to hear from you.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's what all others, including me, going to explain to out thread starter here lol. He added all the code in the main method, and it's the first mess he has.

    I agreed with you on that he/she is not clearly explain what that code is doing.

Similar Threads

  1. Minimize to System Tray In Windows
    By shapeshifter in forum AWT / Swing
    Replies: 4
    Last Post: 12-07-2008, 02:42 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:36 PM
  4. Replies: 1
    Last Post: 04-26-2007, 04:52 PM
  5. Replies: 1
    Last Post: 04-09-2007, 01:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •