title: How to convert java Resultset into Spark dataframe
body: <p>I am trying to use preparestatement with JDBC. It results ResultSet object. I want to convert it into spark dataframe. </p>
  
  <pre><code>object JDBCRead {
  
  val tableName:String = "TABLENAME"
  val url :String = "jdbc:teradata://TERADATA_URL/user=USERNAME,password=PWD,charset=UTF8,TYPE=FASTEXPORT,SESSIONS=10"
  val  selectTable:String  = "SELECT * FROM " + tableName +" sample 10";
  
   val con : Connection = DriverManager.getConnection(url);
  
  
   val pstmt2: PreparedStatement = con.prepareStatement(selectTable)
  
  import java.sql.ResultSet
  
  val rs: ResultSet = pstmt2.executeQuery
  
  
  
  val rsmd: ResultSetMetaData = rs.getMetaData
  while(rs.next()!=null)
  {
    val k: Boolean = rs.next()
    for(i&lt;-1 to rsmd.getColumnCount) {
      print(" " + rs.getObject(i))
    }
    println()
  }
  
  }
  </code></pre>
  
  <p>I want to call above code from Spark Dataframe so that I can load the data into dataframe and get the results faster distributedly.</p>
  
  <p>I must use <code>PreparedStatement</code>. I can not use <code>spark.jdbc.load</code> since FASTEXPORT of Teradata does not work with jdbc load. It has to be used with <code>PreparedStatement</code></p>
  
  <p>How to achieve this? How can I user preparestatement along with SELECT statement to load into Spark Dataframe.</p>