MySQL database installation configuration
In order to see / operate the database easily and intuitively, Navicat Premium software is generally installed to support multiple databases.
There is not much nonsense in the installation process, mainly referring to the following connections, no pit in hand test, which are the main documents:
Navicat Premium 12.0.18 / 12.0.24 installation and activation
https://www.jianshu.com/p/42a33b0dda9c
Navicat Premium 12.0.29 / 12.1.5.0 installation and activation
https://www.jianshu.com/p/5f693b4c9468
Scala operates mysql through JDBC, but not for non JDBC.
Scala operates mysql foundation through JDBC
The following first post others tried simple and feasible, no problem. The quantity of addition, deletion and modification is constant, which is known before input and will not change. Because it is in the program, the content of addition, deletion and modification inevitably contains variables. Later, a simple method will be given to realize it.
package ScalaSQL
import java.sql.{Connection, DriverManager}
object ScalaJDBC {
def main(args: Array[String]) {
val url = "jdbc:mysql://101.18.4.14:2267/aitc_report"
//Val url = "jdbc:mysql://localhost/scala test" if local test
//The main format is jdbc:mysql://IP address / database name“
val driver = "com.mysql.jdbc.Driver"//Driver name
val user = "aitc_report"//User name
val password = "sdkfnhi"//Password
//Initialize data connection
var connection: Connection = null
try {
Class.forName(driver)
connection = DriverManager.getConnection(url,user,password)
val statement = connection.createStatement
//---------------------------------------------------------
val rs = statement.executeQuery("SELECT name, num FROM persons")//To indicate the name of the table in the database (here, persons, num and name are scheme information respectively)
while (rs.next) {
val name = rs.getString("name")
val num = rs.getString("num")
println("name = %s, num = %s".format(name, num))
}//Execute the query statement and return the result
//-------------------------------------------------------
val rs2 = statement.executeUpdate("INSERT INTO `report` (`name`,`num`) VALUES ('zhangsan', '88')")//Insert new data
//-------------------------------------------------------
val rs3 = statement.executeUpdate("UPDATE persons set num=11 WHERE `name`=\"zhangsan\"")//Update and modify existing data
//-------------------------------------------------------
val rs4 = statement.executeUpdate("delete from persons WHERE `name`=\"zhangsan\"")//Existing data deletion
//-------------------------------------------------------
val rs5 = statement.executeUpdate("call add_student(3)")//Perform call stored procedure operation
} catch {
case e: Exception => e.printStackTrace
}
connection.close//Close connection, release resources
Scala operates mysql advanced through JDBC
Scala program running results may contain some variables, at this time, the content of addition, deletion, query and modification will inevitably contain variables. In addition to changing the result to the form of DataFrame as described on the Internet, another simple way is as follows:
val datalist=List(XXXXX)//This part is omitted because of the company
val url="jdbc:mysql://101.18.4.14:2267/aitc_report"
val data = sc.parallelize(datalist)
data.foreachPartition(myFun)
def myFun(iterator: Iterator[(String, Int,Int,Int,Int,Int,Int,Double)]): Unit = { //The parameter types of the function strictly correspond to each other, and then there will be Set
var conn: Connection= null
var ps:java.sql.PreparedStatement=null
val sql="insert into `report_youxi`(`date`,`aitcsender_num`,`sjzssender_num`,`arrive`,`close`,`pop`,`click`,`CTR`)"+"values (?,?,?,?,?,?,?,?)" conn=DriverManager.getConnection(url,"aitc_report","sdkfnhi")
ps = conn.prepareStatement(sql)
iterator.foreach(data => {
ps.setString(1, data._1)
ps.setInt(2, data._2)
ps.setInt(3, data._3)
ps.setInt(4, data._4)
ps.setInt(5, data._5)
ps.setInt(6, data._6)
ps.setInt(7, data._7)
ps.setDouble(8, data._8)
ps.executeUpdate() //Execute Sql statement
})}
Reference resources:
https://blog.csdn.net/rainy_lee/article/details/44562531
The second reference did not find the original link for the time being, found and added.