-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJDBCDemoSimple.java
65 lines (63 loc) · 2.05 KB
/
JDBCDemoSimple.java
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
package facade;
import java.sql.*;
public class JDBCDemoSimple {
public static void main(String[] arg) {
Connection conn = null;
PreparedStatement prep = null;
CallableStatement call = null;
ResultSet rset = null;
try {
Class.forName("<driver>").newInstance();
conn = DriverManager.getConnection("<database>");
String sql = "SELECT * FROM <table> WHERE <column name> =?";
prep = conn.prepareStatement(sql);
prep.setString(1, "<column value>");
rset = prep.executeQuery();
if (rset.next()) {
System.out.println(rset.getString("<column name"));
}
sql = "{call <stored procedure>( ?, ? )}";
call = conn.prepareCall(sql);
call.setInt(1, 1972);
call.registerOutParameter(2, java.sql.Types.INTEGER);
call.execute();
System.out.println(call.getInt(2));
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
if (rset != null) {
try {
rset.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (prep != null) {
try {
prep.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (call != null) {
try {
call.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
}
}