1、Scala连接mysql数据库:

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

object mysqlConn {
  def main(args: Array[String]): Unit = {
    Class.forName("com.mysql.jdbc.Driver")
    val conn: Connection = DriverManager.getConnection("jdbc:mysql://bash/User?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456")
    println(conn)
    val statement: PreparedStatement = conn.prepareStatement("select * from stuInfo")
    val result: ResultSet = statement.executeQuery()
    while (result.next()){
      val number: String = result.getString("stuNumber")
      val name: String = result.getString("stuName")
      val password: String = result.getString("password")
      println(s"学号:${number},姓名:${name},密码:${password}")
    }

    
  }

}

2、Scala连接redis数据库:

import redis.clients.jedis.Jedis

object redisConn {
  def main(args: Array[String]): Unit = {
    //之前创建过redis集群,所以启动集群的里面的redis配置文件,就要启动整个集群
    //启动单独的redis就要关闭集群设置就行
    val jedis = new Jedis("master", 7000)
    jedis.set("name","少帅")
    val name: String = jedis.get("name")
    println(name)
  }

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部