如何使用scala操作数据库
这期内容当中小编将会给大家带来有关如何使用scala 操作数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10余年的延津网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整延津建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“延津网站设计”,“延津网站推广”以来,每个客户项目都认真落实执行。
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() } }
上述就是小编为大家分享的如何使用scala 操作数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
标题名称:如何使用scala操作数据库
当前网址:http://ybzwz.com/article/ppsspe.html