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
    }
  }
}
相关推荐
尘浮生16 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
六月闻君30 分钟前
MySQL 报错:1137 - Can‘t reopen table
数据库·mysql
SelectDB技术团队38 分钟前
兼顾高性能与低成本,浅析 Apache Doris 异步物化视图原理及典型场景
大数据·数据库·数据仓库·数据分析·doris
inventecsh1 小时前
mongodb基础操作
数据库·mongodb
白云如幻1 小时前
SQL99版链接查询语法
数据库·sql·mysql
爱吃烤鸡翅的酸菜鱼1 小时前
MySQL初学之旅(4)表的设计
数据库·sql·mysql·database
计算机毕设指导62 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
The_Ticker2 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Elastic 中国社区官方博客3 小时前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
企鹅侠客3 小时前
ETCD调优
数据库·etcd