forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (49 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"database/sql"
"fmt"
"io"
"os"
_ "github.com/chenyb888/go-ora"
)
func dieOnError(msg string, err error) {
if err != nil {
fmt.Println(msg, err)
os.Exit(1)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("\nhello_sql")
fmt.Println("\thello_sql check if it can connect to the given oracle server using sql interface, then print server banner.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println("\thello_sql oracle://user:pass@server/service_name")
fmt.Println()
os.Exit(1)
}
connStr := os.ExpandEnv(os.Args[1])
conn, err := sql.Open("oracle", connStr)
dieOnError("Can't open connection:", err)
defer conn.Close()
err = conn.Ping()
dieOnError("Can't ping connection:", err)
fmt.Println("\nSuccessfully connected.\n")
stmt, err := conn.Prepare("SELECT * FROM v$version")
dieOnError("Can't prepare query:", err)
defer stmt.Close()
rows, err := stmt.Query()
dieOnError("Can't create query:", err)
defer rows.Close()
for rows.Next() {
var s string
err := rows.Scan(&s)
if err != nil {
break
}
fmt.Println(s)
}
if rows.Err() != nil && rows.Err() != io.EOF {
dieOnError("Can't fetch row:", rows.Err())
}
}