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
    }
  }
}
相关推荐
Tttian62214 分钟前
基于Pycharm与数据库的新闻管理系统(2)Redis
数据库·redis·pycharm
做梦敲代码1 小时前
达梦数据库-读写分离集群部署
数据库·达梦数据库
苹果醋32 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行2 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
hanbarger2 小时前
nosql,Redis,minio,elasticsearch
数据库·redis·nosql
微服务 spring cloud2 小时前
配置PostgreSQL用于集成测试的步骤
数据库·postgresql·集成测试
先睡2 小时前
MySQL的架构设计和设计模式
数据库·mysql·设计模式
弗罗里达老大爷2 小时前
Redis
数据库·redis·缓存
仰望大佬0073 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#