• scala 连接 MySQL 数据库案例


    1 依赖准备

    mysql 8添加:

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>8.0.29version>
    5. dependency>

    mysql 5 添加:

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>5.1.36version>
    5. dependency>

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

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

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

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

            mysql 5 就是 com.mysql.jdbc.Driver

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

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

            这里的的数据库结构是

            建表语句:

    1. show databases ;
    2. create database kb23test;
    3. use kb23test;
    4. create table student(
    5. id int,
    6. name varchar(32),
    7. age int
    8. );
    9. select * from student;
    1. package mysqlstu
    2. import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
    3. class MysqlDemo {
    4. // var driver : String = "com.mysql.jdbc.Driver"
    5. var driver : String = "com.mysql.cj.jdbc.Driver"
    6. // var url = "jdbc:mysql://127.0.0.1:3306"
    7. var url = "jdbc:mysql://39.107.230.113:3306"
    8. var user = "root"
    9. var pwd = "100860"
    10. var connection : Connection = null
    11. def this(driver : String,url : String,user : String,pwd : String){
    12. this()
    13. this.driver = driver
    14. this.url = url
    15. this.user = user
    16. this.pwd = pwd
    17. }
    18. def conn : Connection={
    19. Class.forName(driver)
    20. connection = DriverManager.getConnection(url,user,pwd)
    21. connection
    22. }
    23. def insert():Int = {
    24. val insertSql = "insert into kb23test.student(id,name,age) values(1,'jojo',12)"
    25. if (connection == null) conn
    26. val i : Int = connection.createStatement().executeUpdate(insertSql)
    27. i
    28. }
    29. def insert(id:Int,name:String,age:Int):Int = {
    30. val insertSql = "insert into kb23test.student(id,name,age) values(?,?,?)"
    31. if (connection == null) conn
    32. val pstm : PreparedStatement = connection.prepareStatement(insertSql)
    33. pstm.setInt(1,id)
    34. pstm.setString(2,name)
    35. pstm.setInt(3,age)
    36. val i : Int = pstm.executeUpdate()
    37. i
    38. }
    39. def update(id:Int,name:String,age:Int):Int = {
    40. val insertSql = "update kb23test.student set name = ?,age = ? where id = ?"
    41. if (connection == null) conn
    42. val pstm : PreparedStatement = connection.prepareStatement(insertSql)
    43. pstm.setInt(3,id)
    44. pstm.setString(1,name)
    45. pstm.setInt(2,age)
    46. val i : Int = pstm.executeUpdate()
    47. i
    48. }
    49. def select():Unit={
    50. val selectSql = "select id,name,age from kb23test.student"
    51. if (connection == null)conn
    52. val rs : ResultSet = connection.createStatement().executeQuery(selectSql)
    53. while(rs.next()){
    54. val id : Int = rs.getInt("id")
    55. val name : String = rs.getString("name")
    56. val age : Int = rs.getInt("age")
    57. println("学号:%d 姓名:%s 年龄:%d".format(id,name,age))
    58. }
    59. }
    60. def select(name:String):Unit={
    61. val selectSql = "select id,name,age from kb23test.student where name like concat('%',?,'%')"
    62. if (connection == null)conn
    63. val pstm = connection.prepareStatement(selectSql)
    64. pstm.setString(1,name)
    65. val rs : ResultSet = pstm.executeQuery()
    66. while(rs.next()){
    67. val id : Int = rs.getInt("id")
    68. val name : String = rs.getString("name")
    69. val age : Int = rs.getInt("age")
    70. println("学号:%d 姓名:%s 年龄:%d".format(id,name,age))
    71. }
    72. }
    73. }
    74. object MysqlDemo{
    75. def main(args: Array[String]): Unit = {
    76. val mysqlDemo = new MysqlDemo
    77. println(mysqlDemo.conn)
    78. // println(mysqlDemo.insert())
    79. // println(mysqlDemo.insert(2,"dio",142))
    80. // println(mysqlDemo.update(2,"dio",112))
    81. // mysqlDemo.select("d")
    82. mysqlDemo.select()
    83. }
    84. }

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

    1. package mysqlstu
    2. import java.sql.Connection
    3. class MysqlDemo2 {
    4. // var driver : String = "com.mysql.jdbc.Driver"
    5. var driver: String = "com.mysql.cj.jdbc.Driver"
    6. // var url = "jdbc:mysql://127.0.0.1:3306"
    7. var url = "jdbc:mysql://39.107.230.113:3306"
    8. var user = "root"
    9. var pwd = "100860"
    10. def this(driver: String, url: String, user: String, pwd: String) {
    11. this()
    12. this.driver = driver
    13. this.url = url
    14. this.user = user
    15. this.pwd = pwd
    16. }
    17. }
    18. object MysqlDemo2 {
    19. def main(args: Array[String]): Unit = {
    20. val demo = new MysqlDemo2()
    21. import mysqlstu.MysqlUtils._
    22. demo.insert(3,"kk",11)
    23. }
    24. }
    1. package mysqlstu
    2. import java.sql.{Connection, DriverManager, PreparedStatement}
    3. object MysqlUtils {
    4. implicit class Mysqlop(mysqlDemo2: MysqlDemo2){
    5. private var connection:Connection = _
    6. private def conn():Unit={
    7. Class.forName(mysqlDemo2.driver)
    8. connection = DriverManager.getConnection(mysqlDemo2.url,mysqlDemo2.user,mysqlDemo2.pwd)
    9. }
    10. def insert(id:Int,name:String,age:Int):Int = {
    11. val insertSql = "insert into kb23test.student(id,name,age) values(?,?,?)"
    12. if (connection == null) conn
    13. val pstm : PreparedStatement = connection.prepareStatement(insertSql)
    14. pstm.setInt(1,id)
    15. pstm.setString(2,name)
    16. pstm.setInt(3,age)
    17. val i : Int = pstm.executeUpdate()
    18. i
    19. }
    20. }
    21. }

  • 相关阅读:
    Java并发编程的艺术笔记-Executor框架
    这几个Python实战项目,让大家了解到它的神奇
    【C++】c++11学习-常用特性总结
    Kubernetes部署
    不认识docker,怎么好意思说自己是干IT的
    interceptor和filter的区别
    手把手教你使用LabVIEW OpenCV dnn实现物体识别(Object Detection)含源码
    No such module ‘FacebookCore‘
    算法刷题——爬楼梯
    整合MyBatis-plus
  • 原文地址:https://blog.csdn.net/jojo_oulaoula/article/details/133581100