获取MySQL的JDBC连接对象(Scala版本)
Posted Mr.zhou_Zxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取MySQL的JDBC连接对象(Scala版本)相关的知识,希望对你有一定的参考价值。
获取mysql的JDBC连接对象(Scala版本)
方法一:常规获取
- JDBCUtils
package com.zxy.Utils
import java.sql.{Connection, DriverManager, ResultSet, Statement}
import org.slf4j.LoggerFactory
/**
* @Author zxy
* @Content 创建JDBC连接对象
* @Date 2021-07-10
*/
object JDBCUtils {
private val logger = LoggerFactory.getLogger(JDBCUtils.getClass.getSimpleName)
private val MYSQL_DRIVER = "com.mysql.jdbc.Driver"
private val MYSQL_URL = "jdbc:mysql://localhost:3306/zxy"
private val MYSQL_USER = "root"
private val MYSQL_PWD = "root"
/**
* 创建连接对象
*/
def getMySQLConnection():Connection = {
var connection:Connection = null
try{
Class.forName(MYSQL_DRIVER)
connection = DriverManager.getConnection(MYSQL_URL,MYSQL_USER,MYSQL_PWD)
}catch {
case e:Exception => logger.error("创建JDBC连接对象异常")
}
connection
}
/**
* 关闭Connection对象
*/
def close(connection: Connection):Unit = {
if(connection != null) connection.close()
}
/**
* 测试JDBC创建的连接对象是否成功
* @param args
*/
def main(args: Array[String]): Unit = {
val connection: Connection = JDBCUtils.getMySQLConnection()
val statement: Statement = connection.createStatement()
val sql = "select * from user"
val resultSet: ResultSet = statement.executeQuery(sql)
while (resultSet.next()){
val username: String = resultSet.getString("username")
val password: String = resultSet.getString("password")
println(s"username = ${username},password = ${password}")
}
}
}
方法二:通过配置文件获取
- jdbc.properties
url = jdbc:mysql://localhost:3306/zxy?useSSL=false
user = root
pwd = root
driver = com.mysql.jdbc.Driver
- MySQLUtils
package com.zxy.Utils
import java.io.FileReader
import java.net.URL
import java.sql.{Connection, DriverManager, ResultSet, Statement}
import java.util.Properties
import org.slf4j.LoggerFactory
/**
* @Author zxy
* @Content 借助jdbc.properties获得连接对象
* @Date 2021-07-10
*/
object MySQLUtils {
private val logger = LoggerFactory.getLogger(MySQLUtils.getClass.getSimpleName)
private var DRIVER = ""
private var URL = ""
private var USER = ""
private var PWD = ""
/**
* 获取连接对象
* @return
*/
def getConnection(): Connection ={
val properties = new Properties()
val loader: ClassLoader = MySQLUtils.getClass.getClassLoader
val resource: URL = loader.getResource("jdbc.properties")
val path: String = resource.getPath
properties.load(new FileReader(path))
var connection: Connection = null
try{
URL = properties.getProperty("url")
USER = properties.getProperty("user")
PWD = properties.getProperty("pwd")
DRIVER = properties.getProperty("driver")
Class.forName(DRIVER)
connection = DriverManager.getConnection(URL,USER,PWD)
}catch {
case e:Exception =>logger.error("JDBC连接异常")
}
connection
}
/**
* 关闭Connection
* @param connection
*/
def close(connection: Connection):Unit = {
if(connection != null) connection.close()
}
/**
* 测试连接
* @param args
*/
def main(args: Array[String]): Unit = {
val connection: Connection = MySQLUtils.getConnection()
val statement: Statement = connection.createStatement()
val sql = "select * from user"
val resultSet: ResultSet = statement.executeQuery(sql)
while (resultSet.next()){
val username: String = resultSet.getString("username")
val password: String = resultSet.getString("password")
println(s"username = ${username},password = ${password}")
}
}
}
拓展
/**
* 获取预处理对象,先执行后设置参数
* @param args
*/
def main(args: Array[String]): Unit = {
val connection: Connection = MySQLJDBC.getMySQLConnection()
val sql = "select * from user where username = ? and password = ?"
val ps: PreparedStatement = connection.prepareStatement(sql)
ps.setString(1,"zxy")
ps.setString(2,"zxy")
val resultSet: ResultSet = ps.executeQuery()
while (resultSet.next()){
val username: String = resultSet.getString("username")
val password: String = resultSet.getString("password")
println(s"username = ${username},password = ${password}")
}
}
以上是关于获取MySQL的JDBC连接对象(Scala版本)的主要内容,如果未能解决你的问题,请参考以下文章