scala 操作数据库的方法

所属分类: 软件编程 / java 阅读数: 24
收藏 0 赞 0 分享

1、定义数据库连接

package com.web.dataSource
 
import com.alibaba.druid.pool.DruidDataSource
 
object MySqlDataSource {
 
 val driver = "com.mysql.jdbc.Driver"
 val url = "jdbc:mysql://127.0.0.1:3306"
 val username = "root"
 val password = "root"
 
 val connectionPool = new DruidDataSource()
 connectionPool.setUsername(username)
 connectionPool.setPassword(password)
 connectionPool.setDriverClassName(driver)
 connectionPool.setUrl(url)
 connectionPool.setValidationQuery("select 1")
 connectionPool.setInitialSize(15)
 connectionPool.setMinIdle(10)
 connectionPool.setMaxActive(100)
 connectionPool.setRemoveAbandoned(true)
 connectionPool.setRemoveAbandonedTimeoutMillis(180000)
 connectionPool.setMaxWait(5000)
 connectionPool.setTestOnBorrow(false)
 connectionPool.setTestOnReturn(false)
 
}

2、执行查询

def getOptions(uid:Int) ={
 
 val connection = MySqlDataSource.connectionPool.getConnection
 var sql = 
      s""" select username,password,sex
      |from user 
 
      |where uid = ?
 
      """.stripMargin
 
var stmt = connection.prepareStatement(sql)
 
stmt.setInt(1, uid)
 
var resultSet = stmt.executeQuery()
 
var resultListMap = List[Map[String,String]]()
 
//获取结果
 
while(resultSet.next()){
 
resultListMap = resultListMap :+ Map(
 
                               "username"->resultSet.getString("username"),
 
                               "password"->resultSet.getString("password"),
 
                               "sex"->resultSet.getInt("sex"),
 
                              )
 
}
 
//关闭连接
 
stmt.close()
 
connection .close()
 
//返回结果
 
resultListMap
 
}

3、插入数据

object UpdateLocation {

 def main(args: Array[String]): Unit = {
  val conf = new SparkConf().setAppName("UpdateLocation").setMaster("local[2]")
  val sc = new SparkContext(conf)
  var conn: Connection = null
  var ps: PreparedStatement = null
  try {
   val sql = "INSERT INTO location_info(location,accesse_date,counts) VALUES (?,?,?)"
   conn = DriverManager.getConnection("jdbc:mysql://192.168.126.31:3306/sparkdatabase?useUnicode=true&characterEncoding=utf-8", "root", "Zhm@818919")
   ps = conn.prepareStatement(sql)
   ps.setString(1, "深圳")
   ps.setString(2, "2018-7-2")
   ps.setInt(3, 122)
   ps.execute()
  } catch {
   case e: Exception => println("myException")
  } finally {
   if (conn != null) {
    conn.close()
   }
   if (ps != null) {
    ps.close()
   }
  }
  sc.stop()
 }
}

4、删除操作

object DeleteLocation {

 def main(args: Array[String]): Unit = {
  val conf = new SparkConf().setAppName("UpdateLocation").setMaster("local[2]")
  val sc = new SparkContext(conf)
  var conn: Connection = null
  var ps: PreparedStatement = null
  try {
   val sql = "delete from location_info where location = ?"
   conn = DriverManager.getConnection("jdbc:mysql://192.168.126.31:3306/sparkdatabase?useUnicode=true&characterEncoding=utf-8", "root", "Zhm@818919")
   ps = conn.prepareStatement(sql)
   ps.setString(1, "深圳")
   ps.execute()
  } catch {
   case e: Exception => println("myException")
  } finally {
   if (conn != null) {
    conn.close()
   }
   if (ps != null) {
    ps.close()
   }
  }
  sc.stop()
 }
}

5、更新操作

object InsertLocation {

 def main(args: Array[String]): Unit = {
  val conf = new SparkConf().setAppName("UpdateLocation").setMaster("local[2]")
  val sc = new SparkContext(conf)
  var conn: Connection = null
  var ps: PreparedStatement = null
  try {
   val sql = "update location_info set location=? where id = ?";
   conn = DriverManager.getConnection("jdbc:mysql://192.168.126.31:3306/sparkdatabase?useUnicode=true&characterEncoding=utf-8", "root", "Zhm@818919")
   ps = conn.prepareStatement(sql)
   ps.setString(1, "深圳")
   ps.setInt(2,26)
   ps.execute()
  } catch {
   case e: Exception => println("myException")
  } finally {
   if (conn != null) {
    conn.close()
   }
   if (ps != null) {
    ps.close()
   }
  }
  sc.stop()
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot环境搭建及第一个程序运行(小白教程)

这篇文章主要介绍了SpringBoot环境搭建及第一个程序运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

过滤器 和 拦截器的 6个区别(别再傻傻分不清了)

这篇文章主要介绍了过滤器 和 拦截器的 6个区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

SpringBoot整合SpringTask实现定时任务的流程

这篇文章主要介绍了SpringBoot整合SpringTask实现定时任务的流程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

vscode快速引入第三方jar包发QQ邮件

这篇文章主要介绍了vscode快速引入第三方jar包发QQ邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java Enum和String及int的相互转化示例

这篇文章主要介绍了Java Enum和String及int的相互转化示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring boot如何快速的配置多个Redis数据源

这篇文章主要介绍了Spring boot如何快速的配置多个Redis数据源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 对接腾讯云直播的实现

这篇文章主要介绍了JAVA 对接腾讯云直播的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JavaSE static final及abstract修饰符实例解析

这篇文章主要介绍了JavaSE static final及abstract修饰符实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot定时任务参数运行代码实例解析

这篇文章主要介绍了SpringBoot定时任务运行代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring Boot调用 Shell 脚本实现看门狗功能

这篇文章主要介绍了Spring Boot调用 Shell 脚本实现看门狗功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多