這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何使用scala 操作數(shù)據(jù)庫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10余年的延津網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整延津建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“延津網(wǎng)站設(shè)計”,“延津網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
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()
}
}上述就是小編為大家分享的如何使用scala 操作數(shù)據(jù)庫了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁題目:如何使用scala操作數(shù)據(jù)庫
網(wǎng)站URL:http://chinadenli.net/article34/ppsspe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、營銷型網(wǎng)站建設(shè)、小程序開發(fā)、外貿(mào)建站、網(wǎng)站營銷、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)