标签归档:mysql

RSS feed of mysql

[Spark] MySQL 踩坑记录

scala代码实现连接mysql

首先在 sbt 配置文件里添加:

"mysql" % "mysql-connector-java" % "8.0.12",

踩到的坑

1

使用 JDBC 访问特定数据库时,需要在 spark classpath 上添加对应的 JDBC 驱动配置。

--driver-class-path lib/mysql-connector-java-8.0.12.jar

不然代码在本地 sbt run 运行是没有问题, 但是放在服务器上用 spark-submit 提交的话,可能会报异常:

com.mysql.jdbc.Driver

2

运行项目中提示:

The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

'com.mysql.jdbc.Driver' 驱动类被弃用了,新的驱动类是 'com.mysql.cj.jdbc.Driver'。 这个驱动会通过 SPI 的方式自动加载,通常不需要人工手动加载。

val pool = MysqlConnectionPool(dbHost, dbUser, dbPasswd,  "com.mysql.cj.jdbc.Driver") 

本文链接:/2018/08/14/spark-mysql/
请尊重作者的劳动成果,转载请注明出处!Sakishum 博客保留对文章的所有权利。

Golang:开源库 Go-MySQL-Driver 使用

Golang:开源库 Go-MySQL-Driver 使用

A MySQL-Driver for Go's database/sql package

MySQL

MySQL is the world's most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications. —— MySQL

MySQL驱动 Go-MySQL-Driver 使用

github 地址

Go-MySQL-Driver

文档

Go-MySQL-Driver wiki

安装

$ go get -u github.com/go-sql-driver/mysql

导入

import "database/sql"
import _ "github.com/go-sql-driver/mysql"

数据库表结构

USE test;
CREATE TABLE `squareNum` (
    `number` INT(11) NOT NULL DEFAULT '0',
    `squareNumber` INT(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`number`)
);
/*
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| number       | int(11) | NO   | PRI | NULL    |       |
| squareNumber | int(11) | NO   |     | NULL    |       |
+--------------+---------+------+-----+---------+-------+
*/

用例

package main

import (
        "database/sql"
        "fmt"
        _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "root:11111111@/test?charset=utf8")
    checkErr(err)
        defer db.Close()

    // Open doesn't open a connection. Validate DSN data:
    err = db.Ping()
    checkErr(err)

    // Use the DB normally, execute the querys etc
    stmtIns, err := db.Prepare("INSERT INTO squareNum VALUES( ?, ? )")
    checkErr(err)
    defer stmtIns.Close() // Close the statement when we leave main() / the program terminates

    stmtOut, err := db.Prepare("SELECT squareNumber FROM squareNum WHERE number = ?")
    checkErr(err)
    defer stmtOut.Close()
    for i := 0; i < 25; i++ {
                _, err = stmtIns.Exec(i, (i * i)) // Insert tuples (i, i^2)
                if err != nil {
                        panic(err.Error()) // proper error handling instead of panic in your app
                }
        }

    var squareNum int // we "scan" the result in here

    // Query the square-number of 13
        err = stmtOut.QueryRow(13).Scan(&squareNum) // WHERE number = 13
    checkErr(err)
    fmt.Printf("The square number of 13 is: %d\n", squareNum)

    // Query another number.. 1 maybe?
        err = stmtOut.QueryRow(1).Scan(&squareNum) // WHERE number = 1
        checkErr(err)
        fmt.Printf("The square number of 1 is: %d\n", squareNum)

    stmtIns, err = db.Prepare("DELETE FROM squareNum where number=?")
        checkErr(err)

    res, err := stmtIns.Exec(13)
        checkErr(err)

    affect, err := res.RowsAffected()
        checkErr(err)

    fmt.Println("affect:", affect)
}

func checkErr(err error) {
        if err != nil {
                panic(err.Error())
        }
}

/*
Output:
The square number of 13 is: 169
The square number of 1 is: 1
affect: 1
*/

以上

本文链接:/2018/06/21/golang-go-mysql-driver/
请尊重作者的劳动成果,转载请注明出处!Sakishum 博客保留对文章的所有权利。