ラベル mysql の投稿を表示しています。 すべての投稿を表示
ラベル mysql の投稿を表示しています。 すべての投稿を表示

2012年1月9日月曜日

groovyとmysqlでテーブルのカラムを一覧表示する

groovyとmysqlでテーブルのカラムを一覧表示するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
 "jdbc:mysql://localhost/test?characterEncodint=utf8",
  "root",
  "root",
  "com.mysql.jdbc.Driver")

// カラムを一覧表示する
table = "test1"
query = """
select * from information_schema.columns
where table_name = ${table}
order by ordinal_position
"""
println "table_schema, table_name, column_name, " +
 "column_type, is_nullable"
sql.eachRow(query){
 println("${it.table_schema}, ${it.table_name}, " +
   "${it.column_name}, \"${it.column_type}\", " +
   "${it.is_nullable}")
}

動作環境
groovy 1.8.3, JDK6 Update23, mysql 5.1

2012年1月5日木曜日

groovyとmysqlで現在の接続のユーザ名を取得する

groovyとmysqlで現在の接続のユーザ名を取得するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
 "jdbc:mysql://localhost/test?characterEncoding=utf8",
  "root",
  "root",
  "com.mysql.jdbc.Driver")

// 現在のユーザを表示する
query = """
select current_user() as cu
"""
println "current user:" + sql.firstRow(query).cu

動作環境
groovy 1.8.3, JDK6 Update23, mysql 5.1

2012年1月3日火曜日

groovyとmysqlでBASE TABLE/VIEWを一覧表示する

groovyとmysqlでBASE TABLE/VIEWを一覧表示するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
 "jdbc:mysql://localhost/test?characterEncoding=utf8",
  "root",
  "root",
  "com.mysql.jdbc.Driver")

// BASE TABLE/VIEWを一覧表示する
query = """
select * from information_schema.tables
"""
sql.eachRow(query){
 println("${it.table_schema},${it.table_name},${it.table_type}")
}

動作環境
groovy 1.8.3, JDK6 Update23, mysql 5.1