博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala连接数据库
阅读量:7050 次
发布时间:2019-06-28

本文共 1169 字,大约阅读时间需要 3 分钟。

scala连接数据库

使用JDBC即可:

  • 在sbt中添加对应依赖
libraryDependencies ++= Seq(  "mysql" % "mysql-connector-java" % "5.1.33")
  • 在代码中引用
import java.sql.{Connection, DriverManager}    def insertMysql(date: String, dbDriver: String, dbAdd: String, dbTable: String, dbUser: String, dbPasswd: String, hostApk: String, aid: Long): Unit = {      var connection:Connection = null      try{        Class.forName(dbDriver)        connection = DriverManager.getConnection(dbAdd, dbUser, dbPasswd)        val statement = connection.createStatement()        val delSql = "delete from " + dbTable + " where dt=" + date.toInt + " and hostapk=" + """"""" + hostApk + """""""        val insertSql = "insert into " + dbTable + "(dt, hostapk, aid)" + " values(" + date.toInt + "," + """"""" + hostApk + """"""" + "," + aid + ")"        println("print the SQL statement:")        println(delSql)        println(insertSql)        statement.execute(delSql)        statement.execute(insertSql)      }catch {        case e: Exception => e.printStackTrace()      }      connection.close()    }
  • 对应的参数如下:
dbDriver="com.mysql.jdbc.Driver"dbAddr="jdbc:mysql://127.0.0.1/data"dbTable="hah"dbUser="hi"dbPasswd="nihao"
参考

转载地址:http://ddpol.baihongyu.com/

你可能感兴趣的文章
Eclipse Java开发环境配置 + memcached + maven
查看>>
[转]C++对象内存分配
查看>>
throw和throws区别
查看>>
HNUSTOJ-1257 You are my brother
查看>>
java面试题
查看>>
不用π求坐标夹角大小
查看>>
leetcode 007-009
查看>>
540D - Bad Luck Island(概率DP)
查看>>
java 反射api的功能详解
查看>>
移动web开发之视口viewport
查看>>
web开发中浏览器跨域问题
查看>>
Linux命令常用命令
查看>>
win7电脑数字键盘失灵怎么办
查看>>
java及java web学习笔记
查看>>
SpringMVC+Spring+hibernate整合及分页
查看>>
OpenAI教程
查看>>
前端框架---jQuery---一分钟下载使用
查看>>
Rsync 指令的使用方法
查看>>
LeetCode:459. Repeated Substring Pattern
查看>>
Database Resource website
查看>>