Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SurfMan

Thread: multiplication of two tables using java and jdbc

  1. #1
    abhi140291 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default multiplication of two tables using java and jdbc

    i m trying to calculate the two coulmns of mysql using java and jdbc getting some errors

    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.DatabaseMetaData;
    import java.sql.SQLException;
    import java.io.Console;

    public class BillGen6
    {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DATABASE_URL = "jdbc:mysql://localhost/cscs";
    static final String QUERY1 = "SELECT * from soc1memberbill";
    static final String QUERY2="SELECT* from soc1tarrifcharges";


    public static void main( String args[] )
    {
    Connection connection1 = null; // manages connection
    Statement statement1 = null; // query statement
    Statement statement2=null;

    // connect to database books and query database
    try
    {
    Class.forName( JDBC_DRIVER ); // load database driver class

    // establish connection to database
    connection1 =
    DriverManager.getConnection( DATABASE_URL, "root", "sid123" );

    // create Statement for querying database
    statement1 = connection1.createStatement();
    statement2= connection1.createStatement();

    // query database
    ResultSet resultSet1 = statement1.executeQuery(
    QUERY1 );
    ResultSet resultSet2=statement2.executeQuery(QUERY2);

    // process query results
    ResultSetMetaData metaData1 = resultSet1.getMetaData();
    ResultSetMetaData metaData2 = resultSet2.getMetaData();
    int numberOfColumns1 = metaData1.getColumnCount();
    int numberOfColumns2=metaData2.getColumnCount();
    System.out.println( "Data:" );


    for ( int i = 1; i <= numberOfColumns1; i++ )
    System.out.printf( "%-8s\t", metaData1.getColumnName( i ) );
    System.out.println();
    while ( resultSet1.next() )
    {
    /* for ( int i = 1; i <= numberOfColumns1; i++ )
    System.out.printf( "%-8s\t", resultSet1.getObject( i ) );*/
    String Month_M1=resultSet1.getString("Month_M");
    String MemberName_N=resultSet1.getString("MemberName");
    int Area_A= resultSet1.getInt("Area");
    System.out.println(Month_M1+","+MemberName_N+","+A rea_A );

    System.out.println();
    }

    System.out.println("----");

    for (int i = 1; i <= numberOfColumns2; i++)
    System.out.printf( "%-8s\t",metaData2.getColumnName( i ) );
    System.out.println();
    while ( resultSet2.next() )
    {
    /*for ( int i =1; i <= numberOfColumns2; i++)
    System.out.printf("%-8s\t", resultSet2.getObject( i ) );
    */
    String Month_cs1 = resultSet2.getString("Month_cs");
    String MemberName_N = resultSet2.getString("Societyname");
    int Rpa = resultSet2.getInt("Rateperarea");
    System.out.println( Month_cs1 +", "+MemberName_N+", "+Rpa );
    }
    while (resultSet1.next() && resultSet2.next() )

    {
    for ( int i =1; i <= numberOfColumns2; i++)
    {
    int Area_A= resultSet1.getInt("Area");
    int Rpa = resultSet2.getInt("Rateperarea");


    int Amr = (int)(Area_A * Rpa);
    System.out.println("Amr"+"="+Amr);
    }
    }
    }
    catch ( SQLException sqlException )
    {
    sqlException.printStackTrace();
    System.exit( 1 );
    }
    catch ( ClassNotFoundException classNotFound )
    {
    classNotFound.printStackTrace();
    System.exit( 1 );
    }
    finally // ensure statement and connection are closed properly
    {
    try
    {
    statement1.close();
    statement2.close();
    connection1.close();


    }


    catch ( Exception exception )
    {
    exception.printStackTrace();
    System.exit( 1 );
    }


    }
    }

    }





    here its not considering the while (resultSet1.next() && resultSet2.next() ) this loop for calculation after retriving the data from mysql please help me

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: multiplication of two tables using java and jdbc

    After reading through you code, as good as I could, you have already looped through your resultsets and they are already at their end. Either re-run the query, or use a ResultSet that you can move the internal pointer of (See ResultSet.TYPE_SCROLL SENSITIVE and others).

    When are you going to understand that "doesn't work" does not cut it? In other posts, you have been asked to post the error message itself, the accompanying stacktrace. Post what you got to make things clear. I dropped my crystal ball on the floor last weekend. I ordered a new one, but it is in backorder. Oh, and post your code between [code][/code] tags.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    abhi140291 is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: multiplication of two tables using java and jdbc

    i tried by removing that loop actually i want to multiply two columns from two differant tables in java...is there any other way

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: multiplication of two tables using java and jdbc

    So, you removed a loop and hoped it got solved? If it still works the same after that, then 1) you probably don't need the loop, or 2) something is seriously wrong with your logic.

    What do you mean by "multiplying columns"? Be precise.

    Isn't this something that the database should do? For example:
    Java Code:
    select account.name, account.discount * billing.price as discount_price 
        from account 
        inner join billing on account.id = billing.accountid
    Tolls likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Multiplication Tables game
    By beansnet in forum New To Java
    Replies: 6
    Last Post: 01-04-2013, 04:51 PM
  2. Replies: 9
    Last Post: 12-23-2011, 09:34 PM
  3. jdbc trigger to check other tables
    By towerz in forum Advanced Java
    Replies: 3
    Last Post: 12-23-2011, 01:11 PM
  4. jdbc combobox link between two tables
    By pager in forum Advanced Java
    Replies: 2
    Last Post: 04-27-2011, 05:24 PM
  5. Java Tables
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2010, 05:03 PM

Tags for this Thread

Posting Permissions

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