scala 连接 MySQL 数据库案例

1 依赖准备

mysql 8添加:

XML 复制代码
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.29</version>
    </dependency>

mysql 5 添加:

XML 复制代码
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.36</version>
    </dependency>

当然为了避免下载,可以直接去你的本地仓库查看你以前下过什么版本的依赖

2 连接 mysql 数据库并进行数据操作

这里的driver 换成对应自己版本的驱动

mysql 8 就是 com.mysql.cj.jdbc.Driver

mysql 5 就是 com.mysql.jdbc.Driver

这里的 url 一样换成自己的,若是在服务器上就换成服务器 ip,若在本地就用 localhost 就可以

这里只要能打印出连接,能添加进数据就算成功,其余的无非是 Java 连接数据库的操作了,大差不差了 ,有兴趣的可以自己往下加需求

这里的的数据库结构是

建表语句:

sql 复制代码
show databases ;

create database kb23test;
use kb23test;
create table student(
    id int,
    name varchar(32),
    age int
);

select * from student;
Scala 复制代码
package mysqlstu

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

class MysqlDemo {
//  var driver : String = "com.mysql.jdbc.Driver"
  var driver : String = "com.mysql.cj.jdbc.Driver"
//  var url = "jdbc:mysql://127.0.0.1:3306"
  var url = "jdbc:mysql://39.107.230.113:3306"
  var user = "root"
  var pwd = "100860"

  var connection : Connection = null


  def this(driver : String,url : String,user : String,pwd : String){
    this()
    this.driver = driver
    this.url = url
    this.user = user
    this.pwd = pwd
  }
  def conn : Connection={
    Class.forName(driver)
    connection = DriverManager.getConnection(url,user,pwd)
    connection
  }

  def insert():Int = {
    val insertSql = "insert into kb23test.student(id,name,age) values(1,'jojo',12)"
    if (connection == null) conn
    val i : Int = connection.createStatement().executeUpdate(insertSql)
    i
  }
  def insert(id:Int,name:String,age:Int):Int = {
    val insertSql = "insert into kb23test.student(id,name,age) values(?,?,?)"
    if (connection == null) conn
    val pstm : PreparedStatement = connection.prepareStatement(insertSql)
    pstm.setInt(1,id)
    pstm.setString(2,name)
    pstm.setInt(3,age)
    val i : Int = pstm.executeUpdate()
    i
  }

  def update(id:Int,name:String,age:Int):Int = {
    val insertSql = "update kb23test.student set name = ?,age = ? where id = ?"
    if (connection == null) conn
    val pstm : PreparedStatement = connection.prepareStatement(insertSql)
    pstm.setInt(3,id)
    pstm.setString(1,name)
    pstm.setInt(2,age)
    val i : Int = pstm.executeUpdate()
    i
  }

  def select():Unit={
    val selectSql = "select id,name,age from kb23test.student"
    if (connection == null)conn
    val rs : ResultSet = connection.createStatement().executeQuery(selectSql)
    while(rs.next()){
      val id : Int = rs.getInt("id")
      val name : String = rs.getString("name")
      val age : Int = rs.getInt("age")
      println("学号:%d 姓名:%s 年龄:%d".format(id,name,age))
    }
  }
  def select(name:String):Unit={
    val selectSql = "select id,name,age from kb23test.student where name like concat('%',?,'%')"
    if (connection == null)conn
    val pstm = connection.prepareStatement(selectSql)
    pstm.setString(1,name)
    val rs : ResultSet = pstm.executeQuery()
    while(rs.next()){
      val id : Int = rs.getInt("id")
      val name : String = rs.getString("name")
      val age : Int = rs.getInt("age")
      println("学号:%d 姓名:%s 年龄:%d".format(id,name,age))
    }
  }
}


object MysqlDemo{
  def main(args: Array[String]): Unit = {
    val mysqlDemo = new MysqlDemo
    println(mysqlDemo.conn)

//    println(mysqlDemo.insert())
//    println(mysqlDemo.insert(2,"dio",142))
//    println(mysqlDemo.update(2,"dio",112))
//    mysqlDemo.select("d")
    mysqlDemo.select()
  }
}

3 隐式类形式去实现连接 mysql

Scala 复制代码
package mysqlstu

import java.sql.Connection

class MysqlDemo2 {
  //  var driver : String = "com.mysql.jdbc.Driver"
  var driver: String = "com.mysql.cj.jdbc.Driver"
  //  var url = "jdbc:mysql://127.0.0.1:3306"
  var url = "jdbc:mysql://39.107.230.113:3306"
  var user = "root"
  var pwd = "100860"


  def this(driver: String, url: String, user: String, pwd: String) {
    this()
    this.driver = driver
    this.url = url
    this.user = user
    this.pwd = pwd
  }

}

object MysqlDemo2 {
  def main(args: Array[String]): Unit = {
    val demo = new MysqlDemo2()
    import mysqlstu.MysqlUtils._

    demo.insert(3,"kk",11)
  }
}
Scala 复制代码
package mysqlstu

import java.sql.{Connection, DriverManager, PreparedStatement}

object MysqlUtils {
  implicit class Mysqlop(mysqlDemo2: MysqlDemo2){
    private var connection:Connection = _
    private def conn():Unit={
      Class.forName(mysqlDemo2.driver)
      connection = DriverManager.getConnection(mysqlDemo2.url,mysqlDemo2.user,mysqlDemo2.pwd)
    }

    def insert(id:Int,name:String,age:Int):Int = {
      val insertSql = "insert into kb23test.student(id,name,age) values(?,?,?)"
      if (connection == null) conn
      val pstm : PreparedStatement = connection.prepareStatement(insertSql)
      pstm.setInt(1,id)
      pstm.setString(2,name)
      pstm.setInt(3,age)
      val i : Int = pstm.executeUpdate()
      i
    }
  }
}
相关推荐
时差95311 分钟前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式13 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
秋意钟39 分钟前
MySQL日期类型选择建议
数据库·mysql
Dxy12393102161 小时前
python下载pdf
数据库·python·pdf
ac-er88882 小时前
MySQL如何实现PHP输入安全
mysql·安全·php
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
superman超哥3 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452003 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库
engchina4 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina4 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j