-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJDBCMetadataDemo.java
More file actions
145 lines (138 loc) · 4.74 KB
/
Copy pathJDBCMetadataDemo.java
File metadata and controls
145 lines (138 loc) · 4.74 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.io.IOException;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DatabaseMetaData;
public class JDBCMetadataDemo{
private static final String URL_JAVADB = "jdbc:derby:employee2;create=true";
private static final String URL_SQLITE = "jdbc:sqlite:employee2";
private static final String TB_EMPLOYEES = "EMPLOYEES";
public static void main(String[] args){
if(args.length !=1){
System.err.println("usage 1: java JDBCDemo javadb");
System.err.println("usage 2: java JDBCDemo sqlite");
return;
}
String url = null;
switch(args[0]){
case "javadb":
url = URL_JAVADB;
break;
case "sqlite":
url = URL_SQLITE;
break;
default:
System.err.println("invalid command-line argument");
return;
}
Connection con = null;
try{
if("sqlite".equals(args[0])){
Class.forName("org.sqlite.JDBC"); // load the Driver class file explicitly, regarding non-JAVADB
}
con = DriverManager.getConnection(url);
// EXPLORES THE METADATA
dump(con.getMetaData());
Statement stmt = null;
try{
stmt = con.createStatement();
String sql = null;
if(!isTbExist(con, TB_EMPLOYEES)){
sql = "CREATE TABLE EMPLOYEES(ID INTEGER, NAME VARCHAR(30))"; // STATIC SQL STATEMENT
stmt.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEES VALUES(1, 'Jhon Doe')";
stmt.executeUpdate(sql);
sql = "INSERT INTO EMPLOYEES VALUES(2, 'Sally Smith')";
stmt.executeUpdate(sql);
}
sql = "SELECT * FROM EMPLOYEES";
ResultSet resultSet = stmt.executeQuery(sql);
while(resultSet.next()){
System.out.println(resultSet.getInt("ID") + ", " + resultSet.getString("NAME"));
}
// drop the table
sql = "DROP TABLE EMPLOYEES";
// stmt.executeUpdate(sql);
}catch(SQLException sqle){
while(sqle!=null){
System.err.println("SQL error : " + sqle.getMessage());
System.err.println("SQL state : " + sqle.getSQLState());
System.err.println("Error code : " + sqle.getErrorCode());
System.err.println("Cause : " + sqle.getCause());
sqle = sqle.getNextException();
}
}finally{
if(stmt != null){
try{
stmt.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}catch(SQLException sqle){
while(sqle!=null){
System.err.println("SQL error : " + sqle.getMessage());
System.err.println("SQL state : " + sqle.getSQLState());
System.err.println("Error code : " + sqle.getErrorCode());
System.err.println("Cause : " + sqle.getCause());
sqle = sqle.getNextException();
}
}finally{
if(con != null){
try{
con.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
}
static boolean isTbExist(Connection con, String tbName) throws SQLException{
DatabaseMetaData dbmd = con.getMetaData();
ResultSet resultSet = dbmd.getTables(null, "APP", tbName, null);
return resultSet.next();
}
static void dump(DatabaseMetaData dbmd) throws SQLException{
System.out.println("DB Major version = " + dbmd.getDatabaseMajorVersion());
System.out.println("DB Minor version = " + dbmd.getDatabaseMinorVersion());
System.out.println("DB Product = " + dbmd.getDatabaseProductName());
System.out.println("Driver name = " + dbmd.getDriverName());
System.out.println("Numeric function names from escape clause = " + dbmd.getNumericFunctions());
System.out.println("String function names from escape clause = " + dbmd.getStringFunctions());
System.out.println("System function names from escape clause = " + dbmd.getSystemFunctions());
System.out.println("Time/Date function names from escape clause = " + dbmd.getTimeDateFunctions());
System.out.println("Catalog term = " + dbmd.getCatalogTerm());
System.out.println("Schema term = " + dbmd.getSchemaTerm());
System.out.println();
System.out.println("Catalogs");
System.out.println("--------");
ResultSet resultSet = dbmd.getCatalogs();
while(resultSet.next()){
System.out.println(resultSet.getString("TABLE_CAT"));
}
System.out.println();
System.out.println("Schemas");
System.out.println("--------");
resultSet = dbmd.getSchemas();
while(resultSet.next()){
System.out.println(resultSet.getString("TABLE_SCHEM"));
}
System.out.println();
System.out.println("Schema/Table");
System.out.println("--------");
resultSet = dbmd.getSchemas();
while(resultSet.next()){
String schema = resultSet.getString("TABLE_SCHEM");
ResultSet rs = dbmd.getTables(null, schema, "%", null); // use % as wildcard
while(rs.next()){
System.out.println(schema + " " + rs.getString("TABLE_NAME"));
}
}
}
}