• Scala学习——操作外部数据


    scala操作外部数据

    一、scala读取文件及网络数据

    package top.ruandb.scala.Course08
    import scala.io.Source
     
    object FileApp {
      def main(args: Array[String]): Unit = {
        val FILEPATH = "D:\test\a.txt"
        val URL = "https://www.baidu.com"
        readLine(FILEPATH)
        readChar(FILEPATH)
        readNet(URL)
      }
      //按行读取文件
      def  readLine(path:String): Unit ={
        //通过Source类分fromFile方法读取文件
        val file = Source.fromFile("D:\test\a.txt")(scala.io.Codec.UTF8)
        for(line <- file.getLines()){
          println(line)
        }
      }
      //按char读取文件
      def readChar(path:String): Unit ={
        val file = Source.fromFile("D:\test\a.txt")(scala.io.Codec.UTF8)
        for(ele <- file){
          print(ele)
        }
      }
      //读取网络数据
      def readNet(url:String): Unit ={
        val file = Source.fromURL(url)(scala.io.Codec.UTF8)
        for(line <- file.getLines()){
          println(line)
        }
      }
    }

    二、scala读取mysql数据

    package top.ruandb.scala.Course08
    import java.sql.{Connection, DriverManager, ResultSet, Statement}
    object MysqlApp {
      def main(args: Array[String]): Unit = {
        val url = "jdbc:mysql://192.168.44.181:3306/db_test"
        val username = "root"
        val password = "root"
     
        var connection:Connection = null
        var statement:Statement = null
        var resultSet:ResultSet = null
        try{
          classOf[com.mysql.jdbc.Driver]
          //获取连接
          connection = DriverManager.getConnection(url,username,password);
          statement = connection.createStatement();
          resultSet = statement.executeQuery(" select name,author ,price, publisher from book ")
          while(resultSet.next()){
            val name = resultSet.getString("name")
            val author =resultSet.getString("author")
            val price = resultSet.getString("price")
            val publisher = resultSet.getString("publisher")
            println(s"${name}  ${author}  ${price}  ${publisher}")
          }
        }catch {
          case e:Exception => println(e.printStackTrace())
        }finally {
          if(resultSet != null){
            resultSet.close()
          }
          if(statement != null){
            statement.close()
          }
          if(resultSet != null){
            resultSet.close()
          }
        }
      }
    }

    三、scala读取xml文件

    package top.ruandb.scala.Course08
    import java.io.FileInputStream
    import scala.xml.XML
     
    object XMLApp {
     
      def main(args: Array[String]): Unit = {
    //    loadXml()
        readXmlAttr()
      }
     
      //读取完整的XML文件
      def loadXml(): Unit ={
        //通拓scala.xml.XML读取
    //    val xml = XML.load(this.getClass.getClassLoader.getResource("test.xml"))
    //    println(xml)
        val xml = XML.load(new FileInputStream("D:\IdeaProjects\scala02\src\test.xml"))
        println(xml)
      }
     
      //读取XML属性文件
      def readXmlAttr(): Unit ={
        val xml = XML.load(new FileInputStream("D:\IdeaProjects\scala02\src\test.xml"))
        //读取 header 下的 filed
        val headerFiled = xml  "header"  "field"
        println(headerFiled)//<field required="Y" name="a1">AAA</field><field required="Y" name="a2">BBB</field>
     
        //找所有的field
        val fields = xml \ "field"
        for(field <- fields){
          println(field)
        }
     
        //找header 下 field 的所有name
        //val headerFieldNames = (xml  "header"  "field" ).map(_  "@name")
        val headerFieldNames = xml  "header"  "field" \"@name" //两种都可以
        for(name <- headerFieldNames){
          println(name)
        }
     
        //找到name=ma1的message
        val mes = (xml \ "message").filter(x => (x "@name").text.equals("ma1"))
        for(me <- mes){
          println(me)
        }
      }
    }
  • 相关阅读:
    less css
    Eclipse折叠代码 coffee bytes code folding
    jTDS jdbc驱动
    十点建议:从程序员变企业家 10 Tips for Moving From Programmer to Entrepreneur
    转:Hibernate Query examples (HQL) 示例
    [转]风雨7年话3D 长篇连载
    在游戏中使用CEGUI —— 第一章(底层)
    plusMark(正号硬件性能测试器)
    我会在月底之前将CEGUI相关的东西共享出来
    近期继超女之后的2大新闻
  • 原文地址:https://www.cnblogs.com/jnba/p/10677632.html
Copyright © 2020-2023  润新知