FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/Dynamic_Load_Class/Main.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
Dynamic_Load_Class
/
Main.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
250 lines (223 loc) · 9.53 KB
Breadcrumbs
java-code-example
/
Dynamic_Load_Class
/
Main.java
Copy path
File metadata and controls
executable file
·
250 lines (223 loc) · 9.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Main.java
*
* Created on 1 òðàâíÿ 2007, 14:59
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package
javaapplication8
;
//import com.sun.org.apache.bcel.internal.classfile.JavaClass;
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
IOException
;
import
java
.
lang
.
reflect
.
Constructor
;
import
java
.
lang
.
reflect
.
Method
;
import
java
.
net
.
URL
;
import
java
.
net
.
URLClassLoader
;
import
java
.
sql
.*;
import
java
.
util
.
Properties
;
import
java
.
util
.
jar
.
JarEntry
;
import
java
.
util
.
jar
.
JarInputStream
;
/**
*
* @author Òåõíèê
*/
class
connect_to_firebird
{
public
static
Class
loadClass
(
String
path_to_jar
,
String
class_name
){
Class
return_value
=
null
;
try
{
URLClassLoader
urlLoader
=
new
URLClassLoader
(
new
URL
[]{
new
URL
(
"file"
,
null
,
path_to_jar
)});
return_value
=
urlLoader
.
loadClass
(
class_name
);
System
.
out
.
println
(
"Loaded class:"
+
return_value
.
getName
());
}
catch
(
Throwable
ex
){
System
.
out
.
println
(
" error in load class"
+
ex
.
getMessage
());
};
return
return_value
;
}
connect_to_firebird
(){
java
.
sql
.
Driver
driver
=
null
;
java
.
sql
.
Connection
connection
=
null
;
java
.
sql
.
Statement
statement
=
null
;
java
.
sql
.
ResultSet
rs
=
null
;
java
.
sql
.
ResultSetMetaData
rs_metadata
=
null
;
String
query
;
//String databaseURL = "jdbc:firebirdsql:local:d:/work/sadik/sadik.gdb?sql_dialect=3";
String
databaseURL
=
"jdbc:firebirdsql://localhost:3050/d:/work/sadik/sadik.gdb?sql_dialect=3"
;
String
user
=
"SYSDBA"
;
String
password
=
"masterkey"
;
String
driver_name
=
"org.firebirdsql.jdbc.FBDriver"
;
try
{
System
.
out
.
println
(
"--- try to connect driver"
);
//System.loadLibrary("C:\\jaybird-full-2.1.1.jar");
// ïðîñòîå ïîäêëþ÷åíèå, êîòîðîå òðåáóåò â äèðåêòîðèè LIB æåñòêî ïðèâÿçàííîãî JAR
//Class.forName(class_name);
// Äèíàìè÷åñêîå ïîäêëþ÷åíèå:
// 1:
//driver=new org.firebirdsql.jdbc.FBDriver();
// 2:
/*
Class current_class=loadClass("C:\\jaybird-full-2.1.1.jar","org.firebirdsql.jdbc.FBDriver");
System.out.println("--- Class:"+current_class.toString());
Constructor[] constr=current_class.getConstructors();
for(int counter=0;counter<constr.length;counter++){
System.out.println(counter+" : "+constr[counter].getName());
}
Object object=current_class.newInstance();
driver=(java.sql.Driver)current_class.newInstance();
System.out.println("--- Driver:"+driver.toString());
java.sql.DriverManager.registerDriver(driver);
*/
// 3:
/*
URL url=new URL("jar:file:C:/jaybird-full-2.1.1.jar!/");
URLClassLoader class_loader=new URLClassLoader(new URL[]{url});
System.out.println("URLClassLoader:"+class_loader);
Class.forName(class_name,true,class_loader);
System.out.println("loaded class");
*/
// 4:
//URL url = new URL("jar:file:/c:/jaybird-full-2.1.1.jar!/");
URL
url
=
new
URL
(
"file"
,
null
,
"C:
\\
jaybird-full-2.1.1.jar"
);
URLClassLoader
ucl
=
new
URLClassLoader
(
new
URL
[]{
url
},
null
);
System
.
out
.
println
(
"---URLClassLoader:"
+
ucl
);
Class
class_value
=
Class
.
forName
(
driver_name
,
true
,
ucl
);
System
.
out
.
println
(
"---classValue:"
+
class_value
);
Object
d_object
=
class_value
.
newInstance
();
System
.
out
.
println
(
"---object Driver:"
+
d_object
);
driver
= (
Driver
)
class_value
.
newInstance
();
System
.
out
.
println
(
"---to Driver:"
+
driver
);
DriverManager
.
registerDriver
(
driver
);
System
.
out
.
println
(
"---register Driver"
);
Properties
properties
=
new
Properties
();
properties
.
put
(
"user"
,
user
);
properties
.
put
(
"password"
,
password
);
//org.firebirdsql.pool.FBWrappingDataSource dataSource=new org.firebirdsql.pool.FBWrappingDataSource();
//connection=java.sql.DriverManager.getConnection(databaseURL,user,password);
connection
=
driver
.
connect
(
databaseURL
,
properties
);
System
.
out
.
println
(
"firebird connected"
);
statement
=
connection
.
createStatement
();
query
=
"SELECT KOD_1NF FROM S_DDZ"
;
if
(
statement
.
execute
(
query
)==
true
){
System
.
out
.
println
(
"Query OK"
);
// âûâîä ðåçóëüòàòà
rs
=
statement
.
getResultSet
();
// âûâîä çàãîëîâêîâ
rs_metadata
=
rs
.
getMetaData
();
for
(
int
i
=
0
;
i
<
rs_metadata
.
getColumnCount
();
i
++){
System
.
out
.
print
(
" >"
+
rs_metadata
.
getColumnLabel
(
i
+
1
)+
"< "
);
}
System
.
out
.
println
(
"
\n
"
);
// âûâîä äàííûõ
while
(
rs
.
next
()){
for
(
int
i
=
0
;
i
<
rs_metadata
.
getColumnCount
();
i
++){
System
.
out
.
print
(
" "
+
rs
.
getInt
(
i
+
1
)+
" "
);
}
System
.
out
.
println
(
"
\n
"
);
}
System
.
out
.
println
(
"end of MySQL"
);
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
">>>Error "
+
e
.
getMessage
());
}
catch
(
Throwable
ex
){
System
.
out
.
println
(
">>>Throwable: "
+
ex
.
getMessage
());
}
}
}
class
connect_to_mysql
{
String
driver_name
=
"org.gjt.mm.mysql.Driver"
;
java
.
sql
.
Connection
connection
=
null
;
java
.
sql
.
Driver
driver
=
null
;
java
.
sql
.
Statement
statement
=
null
;
java
.
sql
.
ResultSet
rs
=
null
;
java
.
sql
.
ResultSetMetaData
rs_metadata
=
null
;
String
query
;
//String driver="org.gjt.mm.mysql.Driver";
connect_to_mysql
(){
try
{
// ïîïûòêà ïîäêëþ÷åíèÿ ê áàçå äàííûõ
Class
.
forName
(
driver_name
);
connection
=
java
.
sql
.
DriverManager
.
getConnection
(
"jdbc:mysql://localhost:3306/db_temp"
,
"root"
,
""
);
System
.
out
.
println
(
"connected"
);
statement
=
connection
.
createStatement
();
query
=
"SELECT * FROM TABLE_1"
;
if
(
statement
.
execute
(
query
)==
true
){
System
.
out
.
println
(
"Query OK"
);
// âûâîä ðåçóëüòàòà
rs
=
statement
.
getResultSet
();
// âûâîä çàãîëîâêîâ
rs_metadata
=
rs
.
getMetaData
();
for
(
int
i
=
0
;
i
<
rs_metadata
.
getColumnCount
();
i
++){
System
.
out
.
print
(
" >"
+
rs_metadata
.
getColumnLabel
(
i
+
1
)+
"< "
);
}
System
.
out
.
println
(
"
\n
"
);
// âûâîä äàííûõ
while
(
rs
.
next
()){
for
(
int
i
=
0
;
i
<
rs_metadata
.
getColumnCount
();
i
++){
System
.
out
.
print
(
" "
+
rs
.
getString
(
i
+
1
)+
" "
);
}
System
.
out
.
println
(
"
\n
"
);
}
System
.
out
.
println
(
"end of MySQL"
);
}
else
{
System
.
out
.
println
(
"Error in query"
);
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"Error in connect to MySQl
\n
"
+
e
.
getMessage
());
}
}
}
public
class
Main
{
/** Creates a new instance of Main */
public
Main
() {
}
/**
* @param args the command line arguments
*/
public
static
void
main
(
String
[]
args
) {
// TODO code application logic here
try
{
//new connect_to_mysql();
new
connect_to_firebird
();
}
catch
(
java
.
lang
.
NoClassDefFoundError
e
){
System
.
out
.
println
(
">>> Error in thread main:"
+
e
.
getMessage
());
}
}
private
static
URLClassLoader
getURLClassLoader
(
URL
jarURL
) {
return
new
URLClassLoader
(
new
URL
[]{
jarURL
});
}
public
static
void
addJAR
(
String
path_to_file
,
String
class_name
){
try
{
URLClassLoader
urlLoader
=
getURLClassLoader
(
new
URL
(
"file"
,
null
,
path_to_file
));
Class
class_value
=
urlLoader
.
loadClass
(
class_name
);
System
.
out
.
println
(
"Loaded class:"
+
class_value
.
getName
());
JarInputStream
jis
=
new
JarInputStream
(
new
FileInputStream
(
path_to_file
));
JarEntry
entry
=
jis
.
getNextJarEntry
();
int
loadedCount
=
0
,
totalCount
=
0
;
}
catch
(
Throwable
ex
){
System
.
out
.
println
(
" error in load class"
+
ex
.
getMessage
());
};
}
public
static
void
addURL
(
URL
u
)
throws
IOException
{
URLClassLoader
sysLoader
= (
URLClassLoader
)
ClassLoader
.
getSystemClassLoader
();
URL
urls
[] =
sysLoader
.
getURLs
();
for
(
int
i
=
0
;
i
<
urls
.
length
;
i
++) {
if
(
urls
[
i
].
toString
().
equalsIgnoreCase
(
u
.
toString
())) {
System
.
out
.
println
(
"URL "
+
u
+
" is already in the CLASSPATH"
);
return
;
}
}
/*Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysLoader, new Object[]{u});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}*/
}
}
Back
|
FazBrowse Home
|
New Git URL