title: JDBC Create a copy of a table with executeQuery() using only Java
body: <p>I need to print four columns from a copied database and I am required to use only JDBC. The work of printing the columns all neatly formatted is something I think I can handle. Its the creating the copy of the table and then inserting the required rows that is giving me difficulty. Is it possible to write executeQuery() strings to accomplish this? I know to INSERT the rows I need to use executeUpdate(). I would be grateful for any help or direction. Thanks.</p>
  
  <pre><code>    public static void main(String args[]) throws SQLException {
      // define common JDBC objects
    Connection connection = null;
    Statement create = null;
    Statement insert = null;
    ResultSet rs = null;
  
        // Connect to the database
        String dbUrl = 
            "jdbc:oracle:xxxxxxxxxxxxxxxxxxxxxxxxxx";
        String username = args[0];
        String password = args[1];
        Collection&lt;String&gt; result = new ArrayList&lt;String&gt;();
        connection = DriverManager.getConnection(
            dbUrl, username, password);
        create = connection.createStatement();
        String query =
                "CREATE TABLE newTable AS " 
              + "SELECT * " 
              + "FROM oldTable; ";          
        rs = create.executeQuery(query);
  
        insert = connection.createStatement();          
        int rows = insert.executeUpdate("INSERT INTO newTable SELECT * FROM oldTable ");
        if (rows == 0) {
          System.out.println("No rows");
        } else {
          System.out.println(rows + " row(s)affected.");
          connection.close();
        }
      }
    }
  
   //and here is the combined sql statement with CREATE and INSERT:
  statement = connection.createStatement();
  sql = 
  "CREATE TABLE newTable AS SELECT * FROM oldTable "
  + "INSERT INTO newTable "
  + "(" + " col01,col02,col03,col04, "
  + "col05,col06,col07,col08,col09, "
  + "col10,col11,col12 " + ") "
  + "VALUES " + "(" + "'data01','data02','data03','data04', "
  + "data05,data06,data07,'data08',data09,data10,data11,data12 " +")" ;
     statement.executeUpdate(sql); 
  </code></pre>