Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default problem in inserting values into third table

    /*DB script:
    create table soc1memberbill (Month_M varchar(20),

    MemberName varchar(20), Area int);
    insert into soc1memberbill values ('jan' , 'ram', 400);
    create table soc1tarrifcharges(Month_cs varchar

    (20),SocietyName varchar(20),Rateperarea int);
    insert into soc1tarrifcharges values ('jan' , 'raheja', 2);
    create table billtest(Month_M varchar(20) , Membername

    varchar(20) ,total int);*\








    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 BillGen14
    {
    // 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();
    for (int i = 1; i <= numberOfColumns2; i++)
    //System.out.printf( "%-8s

    \t",metaData2.getColumnName( i ) );
    System.out.println();
    while ( resultSet1.next() )
    {
    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 Society = resultSet2.getString("Societyname");
    int Rpa = resultSet2.getInt("Rateperarea");

    System.out.println( "Rpa"+"=" +Rpa );


    /* 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("Area"+"="+Area_A );
    int Total = Area_A*Rpa;
    System.out.println("Total"+"=" +Total);
    System.out.println("Month_M1"+"=" +Month_M1);
    System.out.println

    ("MemberName"+"="+MemberName_N);
    Statement stmt3 = null;

    stmt3 = connection1.createStatement();
    String insertQuery = "insert into billtest values

    ('"+MemberName_N+"' , '"+Month_M1+"', "+Total+")";
    System.out.println("insertQuery= "+insertQuery);
    //int done = stmt3.executeUpdate(insertQuery);
    ResultSet resultset3= stmt3.executeQuery(insertQuery);
    System.out.println("done= ");


    //ResultSet uprs = stmt3.executeQuery("insert into testbill

    values ('MemberName_N' , 'Month_M1', Total)");
    //System.out.println("audhu");
    //uprs.moveToInsertRow();
    //uprs.updateString("Month_M",Month_M1 );
    //uprs.updateString("MemberName",MemberName_N );
    // uprs.updateint("Total",Total);

    //uprs.insertRow();
    // uprs.beforeFirst();






    System.out.println();
    }
    }






    }
    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();
    //stmt3.close();
    connection1.close();


    }


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


    }
    }

    }

    guise i have created 3 tables as mentioned above . i have to take values from first two table and insert it into third table.... its showing error pls suggest what to do to removejava.sql.SQLException: Can not issue data manipulation statements with executeQuery().
    Rpa=2
    Area=400
    Total=800
    Month_M1=jan
    MemberName=ram
    insertQuery= insert into billtest values ('ram' , 'jan', 800)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:919)
    at com.mysql.jdbc.StatementImpl.checkForDml(Statement Impl.java:504)
    at com.mysql.jdbc.StatementImpl.executeQuery(Statemen tImpl.java:1523)
    at billgen12.BillGen12.main(BillGen12.java:94)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    my error

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: problem in inserting values into third table

    Quote Originally Posted by niraj kr singh View Post
    Can not issue data manipulation statements with executeQuery().
    This is clear as water: the executeQuery() method can't execute those type of statements; read the API docs for the Statement interface and find another 'execute' method that can execute inserts, upates and deletes ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 3
    Last Post: 10-18-2012, 03:00 PM
  2. problem with inserting into table
    By pardhu in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2011, 05:46 AM
  3. problem with inserting into table
    By pardhu in forum JDBC
    Replies: 1
    Last Post: 11-19-2011, 05:42 AM
  4. Problem in inserting values into Oracle DB
    By techsing14 in forum Java Servlet
    Replies: 4
    Last Post: 04-13-2011, 01:52 AM
  5. Replies: 0
    Last Post: 12-24-2007, 10:47 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
  •