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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.PreparedStatement
import java.sql.ResultSetMetaData
import java.sql.SQLException
public class TestDemo {
public static Connection getConnection() {
Connection conn = null
try {
Class.forName("com.mysql.jdbc.Driver")
String url = "jdbc:mysql://数据库IP地址:3306/数据库名称"
String user = "数据库用户名"
String pass = "数据库用户密码"
conn = DriverManager.getConnection(url, user, pass)
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
return conn
}
public static void main(String[] args) {
Connection conn = getConnection()
String sql = "select * from AccessType"
PreparedStatement stmt
try {
stmt = conn.prepareStatement(sql)
ResultSet rs = stmt.executeQuery(sql)
ResultSetMetaData data = rs.getMetaData()
for (int i = 1i <= data.getColumnCount()i++) {
// 获得所有列的数目及实际列数
int columnCount = data.getColumnCount()
// 获得指定列的列名
String columnName = data.getColumnName(i)
// 获得指定列的列值
int columnType = data.getColumnType(i)
// 获得指定列的数据类型名
String columnTypeName = data.getColumnTypeName(i)
// 所在的Catalog名字
String catalogName = data.getCatalogName(i)
// 对应数据类型的类
String columnClassName = data.getColumnClassName(i)
// 在数据库中类型的最大字符个数
int columnDisplaySize = data.getColumnDisplaySize(i)
// 默认的列的标题
String columnLabel = data.getColumnLabel(i)
// 获得列的模式
String schemaName = data.getSchemaName(i)
// 某列类型的精确度(类型的长度)
int precision = data.getPrecision(i)
// 小数点后的位数
int scale = data.getScale(i)
// 获取某列对应的表名
String tableName = data.getTableName(i)
// 是否自动递增
boolean isAutoInctement = data.isAutoIncrement(i)
// 在数据库中是否为货币型
boolean isCurrency = data.isCurrency(i)
// 是否为空
int isNullable = data.isNullable(i)
// 是否为只读
boolean isReadOnly = data.isReadOnly(i)
// 能否出现在where中
boolean isSearchable = data.isSearchable(i)
System.out.println(columnCount)
System.out.println("获得列" + i + "的字段名称:" + columnName)
System.out.println("获得列" + i + "的类型,返回SqlType中的编号:"+ columnType)
System.out.println("获得列" + i + "的数据类型名:" + columnTypeName)
System.out.println("获得列" + i + "所在的Catalog名字:"+ catalogName)
System.out.println("获得列" + i + "对应数据类型的类:"+ columnClassName)
System.out.println("获得列" + i + "在数据库中类型的最大字符个数:"+ columnDisplaySize)
System.out.println("获得列" + i + "的默认的列的标题:" + columnLabel)
System.out.println("获得列" + i + "的模式:" + schemaName)
System.out.println("获得列" + i + "类型的精确度(类型的长度):" + precision)
System.out.println("获得列" + i + "小数点后的位数:" + scale)
System.out.println("获得列" + i + "对应的表名:" + tableName)
System.out.println("获得列" + i + "是否自动递增:" + isAutoInctement)
System.out.println("获得列" + i + "在数据库中是否为货币型:" + isCurrency)
System.out.println("获得列" + i + "是否为空:" + isNullable)
System.out.println("获得列" + i + "是否为只读:" + isReadOnly)
System.out.println("获得列" + i + "能否出现在where中:"+ isSearchable)
}
} catch (SQLException e) {
e.printStackTrace()
}
}
}
经过access测试,okString url = "jdbc:odbc:test"//最后一个为数据库名
Statement sm = null
String command = null
ResultSet rs = null
String tableName = null
String cName = null
String result = null
String []pram = new String[1]
List <String>tables = new ArrayList<String>()
pram[0] = "table"
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in))
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")// 加载驱动
} catch (ClassNotFoundException e) {
System.out.println("Can not load Jdbc-Odbc Bridge Driver")
System.err.print("ClassNotFoundException:")
System.err.println(e.getMessage())
}
Connection con = DriverManager.getConnection(url, "admin", "admin")// 连接到数据库
DatabaseMetaData dm = con.getMetaData()
rs = dm.getTables(null, null, null, pram)//查找所有的表
while(rs.next()){
tables.add(rs.getString(3))
}
rs.close()
for(String tb : tables){
rs = dm.getColumns(null, null, tb, null)//查找当前表的字段
System.out.println("\nthe table is: " + tb)
ResultSetMetaData rsmd = rs.getMetaData()
int len, type
len = rsmd.getColumnCount()
// System.out.println(len)
for(int i = 1i <= leni ++){
type = rsmd.getColumnType(i)
//这里是获取了一个字段类型的int型,需要转化成string的话要做一个swtich,就不转了,你自己看java.sql.Types这个类去
System.out.print("\t" + rsmd.getColumnName(i) + ": " + rsmd.getColumnType(i))
}
}
rs.close()
con.close()
// }
} catch (SQLException ex) {
System.out.println("SQLException:")
while (ex != null) {
System.out.println("Message:" + ex.getMessage())
ex = ex.getNextException()
}
} catch (Exception e) {
System.out.println("IOException")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)