1、引入驱动
go_ora "github.com/sijms/go-ora/v2"
2、服务名连接方式
conn, err := go_ora.NewConnection(fmt.Sprintf("oracle://%s:%s@%s:%d/%s", "system", "123456", "172.16.107.34", 1521, "oracle11"))
if err != nil {
log.Println("[数据库连接失败]:", err)
return
}
err = conn.Open()
if err != nil {
log.Println("[数据库连接失败]:", err)
return
}
defer conn.Close()
if conn == nil {
log.Println("connect failure")
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err = conn.Ping(ctx)
if err != nil {
log.Println("connect failure 2")
return
}
log.Println("success")
3、Sid方式连接
conn, err := go_ora.NewConnection(fmt.Sprintf("oracle://%s:%s@%s:%d?SID=%s", "system", "12345689", "192.168.10.34", 1521, "orcl"))
if err != nil {
log.Println("[数据库连接失败]:", err)
return
}
err = conn.Open()
if err != nil {
log.Println("[数据库连接失败]:", err)
return
}
defer conn.Close()
if conn == nil {
log.Println("connect failure")
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err = conn.Ping(ctx)
if err != nil {
log.Println("connect failure 2")
return
}
log.Println("success")