scala实现读取Oracle数据

用scala实现读取oracle数据

增加oralce的jar包后

package cn.bigdata.scala.oracle

import java.sql.{DriverManager, Connection, ResultSet}

object ConnectOracle {
 
   val user="youname"
    val password = "youpwd"
    val conn_str = "jdbc:oracle:thin:@ip:1521:database";

  println(conn_str)
   def main(args:Array[String]): Unit ={
    //classOf[com.mysql.jdbc.Driver]
    Class.forName("oracle.jdbc.OracleDriver")//.newInstance();
    val conn = DriverManager.getConnection(conn_str,user, password)
    println("hello")
    try {
      val st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
      // Execute Query
       val rs = st.executeQuery("select *  from table t where t.Fdsid = '1002'")
      // Iterate Over ResultSet
      while (rs.next) {
        println(rs.getString("cloumn"))
      }
    }
    catch {
      case _ : Exception => println("===>")
    }
    finally {
      conn.close
    }
  }
}    

  

posted @ 2015-04-21 11:03  zhanggl  阅读(1251)  评论(0编辑  收藏  举报