scala 操作數(shù)據(jù)庫的方法
更新時間:2019年06月19日 10:33:30 作者:張樂1993
這篇文章主要介紹了scala 操作數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
1、定義數(shù)據(jù)庫連接
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、執(zhí)行查詢
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]]()
//獲取結(jié)果
while(resultSet.next()){
resultListMap = resultListMap :+ Map(
"username"->resultSet.getString("username"),
"password"->resultSet.getString("password"),
"sex"->resultSet.getInt("sex"),
)
}
//關(guān)閉連接
stmt.close()
connection .close()
//返回結(jié)果
resultListMap
}
3、插入數(shù)據(jù)
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()
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于FeignException$InternalServerError的解決方案
這篇文章主要介紹了FeignException$InternalServerError的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Netty學(xué)習(xí)之理解selector原理示例
這篇文章主要為大家介紹了Netty學(xué)習(xí)之理解selector原理示例使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-07-07
SpringBoot動態(tài)修改yml配置文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot動態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

