Skip to content

Commit e8655fd

Browse files
author
turingfly
committed
JDBC
1 parent 8fdb077 commit e8655fd

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package jdbc;
2+
3+
import java.sql.*;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import com.sun.corba.se.pept.transport.Connection;
8+
9+
/**
10+
*
11+
* @author chengfeili
12+
* Jun 14, 2017 5:30:14 PM
13+
*
14+
* Driver: Knows how to get a connection to the database
15+
* Connection: Knows how to communicate with the database
16+
* Statement: Knows how to run the SQL
17+
* ResultSet: Knows what was returned by a SELECT query
18+
*
19+
* In real applications, you should use a DataSource rather than
20+
* DriverManager to get a Connection. For one thing, there’s no reason
21+
* why you should have to know the database password. It’s far better if
22+
* the database team or another team can set up a data source that you
23+
* can reference. Another reason is that a DataSource maintains a
24+
* connection pool so that you can keep reusing the same connection
25+
* rather than needing to get a new one each time.
26+
*/
27+
public class ConnectingADataBase {
28+
public void connect() throws SQLException {
29+
Connection conn = (Connection) DriverManager.getConnection("jdbc:derby:zoo");
30+
System.out.println(conn);
31+
32+
Connection conn1 = (Connection) DriverManager.getConnection("jdbc:postgresql://localhost:5432/ocp-book",
33+
"username", "password");
34+
System.out.println(conn1);
35+
}
36+
37+
// Statement: Knows how to run the SQL
38+
public void obtainingAStatement() throws SQLException {
39+
Connection conn = (Connection) DriverManager.getConnection("jdbc:derby:zoo");
40+
41+
/**
42+
* Parameters: resultSetType a result set type; one of
43+
* ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or
44+
* ResultSet.TYPE_SCROLL_SENSITIVE resultSetConcurrency a concurrency
45+
* type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
46+
*/
47+
Statement stmt = ((java.sql.Connection) conn).createStatement(ResultSet.TYPE_FORWARD_ONLY,
48+
ResultSet.CONCUR_READ_ONLY);
49+
50+
}
51+
52+
public void executingAStatement() throws SQLException {
53+
Connection conn = (Connection) DriverManager.getConnection("jdbc:derby:zoo");
54+
Statement stmt = ((java.sql.Connection) conn).createStatement();
55+
int result = stmt.executeUpdate("insert into species values(10, 'Deer', 3)");
56+
System.out.println(result); // 1
57+
result = stmt.executeUpdate("update species set name = '' where name = 'None'");
58+
System.out.println(result); // 0
59+
result = stmt.executeUpdate("delete from species where id =10");
60+
System.out.println(result); // 1
61+
62+
/**
63+
* If sql is a SELECT , the boolean is true and we can get the ResultSet
64+
* . If it is not a SELECT , we can get the number of rows updated.
65+
*/
66+
boolean isResultSet = stmt.execute("sql");
67+
if (isResultSet) {
68+
ResultSet rs = stmt.getResultSet();
69+
System.out.println("ran a query");
70+
} else {
71+
int result1 = stmt.getUpdateCount();
72+
System.out.println("ran an update");
73+
}
74+
}
75+
76+
public void readingAResultSet() throws SQLException {
77+
Map<Integer, String> idToNameMap = new HashMap<>();
78+
Connection conn = (Connection) DriverManager.getConnection("jdbc:derby:zoo");
79+
Statement stmt = ((java.sql.Connection) conn).createStatement();
80+
ResultSet rs = stmt.executeQuery("select id, name from species");
81+
while (rs.next()) {
82+
int id = rs.getInt("id");
83+
String name = rs.getString("name");
84+
// int id = rs.getInt(1); // first column
85+
// String name = rs.getString(2);
86+
idToNameMap.put(id, name);
87+
}
88+
// getObject()
89+
while (rs.next()) {
90+
Object idField = rs.getObject("id");
91+
Object nameField = rs.getObject("name");
92+
if (idField instanceof Integer) {
93+
int id = (Integer) idField;
94+
System.out.println(id);
95+
}
96+
if (nameField instanceof String) {
97+
String name = (String) nameField;
98+
System.out.println(name);
99+
}
100+
System.out.println(idToNameMap); // {1=African Elephant, 2=Zebra}
101+
}
102+
rs.afterLast();
103+
System.out.println(rs.previous()); // last row
104+
System.out.println(rs.first());
105+
System.out.println(rs.last());
106+
System.out.println(rs.absolute(1)); // first row
107+
System.out.println(rs.absolute(-1)); // last row
108+
}
109+
110+
// closing a Database
111+
public void closingDataBase() throws SQLException {
112+
String url = " jdbc:derby:zoo";
113+
try (Connection conn = (Connection) DriverManager.getConnection(url);
114+
Statement stmt = conn.createStatement();
115+
ResultSet rs = stmt.executeQuery("select name from animal")) {
116+
while (rs.next())
117+
System.out.println(rs.getString(1));
118+
}
119+
}
120+
121+
// closing a Database prior to Java 7
122+
public void closingDataBase1() throws SQLException {
123+
String url = "jdbc:derby:zoo";
124+
Connection conn = null;
125+
Statement stmt = null;
126+
ResultSet rs = null;
127+
try {
128+
conn = (Connection) DriverManager.getConnection(url);
129+
stmt = ((java.sql.Connection) conn).createStatement();
130+
rs = stmt.executeQuery("select name from animal");
131+
while (rs.next())
132+
System.out.println(rs.getString(1));
133+
} finally
134+
135+
{
136+
closeResultSet(rs);
137+
closeStatement(stmt);
138+
closeConnection(conn);
139+
}
140+
}
141+
142+
private void closeResultSet(ResultSet rs) {
143+
try {
144+
if (rs != null)
145+
rs.close();
146+
} catch (SQLException e) {
147+
}
148+
}
149+
150+
private void closeStatement(Statement stmt) {
151+
try {
152+
if (stmt != null)
153+
stmt.close();
154+
} catch (SQLException e) {
155+
}
156+
}
157+
158+
private void closeConnection(Connection conn) {
159+
try {
160+
if (conn != null)
161+
conn.close();
162+
} catch (SQLException e) {
163+
}
164+
}
165+
166+
public void dealingWithExceptions() {
167+
String url = " jdbc:derby:zoo";
168+
try (Connection conn = DriverManager.getConnection(url);
169+
Statement stmt = conn.createStatement();
170+
ResultSet rs = stmt.executeQuery("select not_a_column from animal")) {
171+
while (rs.next())
172+
System.out.println(rs.getString(1));
173+
} catch (SQLException e) {
174+
System.out.println(e.getMessage());
175+
System.out.println(e.getSQLState());
176+
System.out.println(e.getErrorCode());
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)