我先发表一段代码,有一段看的不是很懂,
01.import java.sql.Connection;
02.import java.sql.DriverManager;
03.import java.sql.ResultSet;
04.import java.sql.SQLException;
05.import java.sql.Statement;
06.
07.
08.public class DBTools {
09.// 定义一个方法,用来得到一个"新的"连接对象
10.public static Connection getConnection()
11.{
12.Connection conn = null;
13.String driverName = "oracle.jdbc.driver.OracleDriver";
14.String url = "jdbc:oracle:thin:@localhost:1521:ora9i";
15.String userName = "scott";
16.String passWord = "tiger";
17.try {
18.Class.forName(driverName);
19.conn = DriverManager.getConnection(url,userName,passWord );
20.} catch (Exception e) {
21.// TODO Auto-generated catch block
22.e.printStackTrace();
23.}
24.return conn;
25.}
26.
27.
28.public static void closeConn(Connection conn)
29.{
30.try {
31.if(conn != null)
32.{
33.conn.close();
34.}
35.} catch (SQLException e) {
36.// TODO Auto-generated catch block
37.e.printStackTrace();
38.}
39.}
40.
41.
42.public static void closeState(Statement state)
43.{
44.try {
45.if(state != null)
46.{
47.state.close();
48.}
49.} catch (SQLException e) {
50.// TODO Auto-generated catch block
51.e.printStackTrace();
52.}
53.}
54.
55.
56.public static void closeRs(ResultSet rs)
57.{
58.try {
59.if(rs != null)
60.{
61.rs.close();
62.}
63.} catch (SQLException e) {
64.// TODO Auto-generated catch block
65.e.printStackTrace();
66.}
67.}
68.}
69.
70.
71.
72.
73.
74.
75.import java.sql.ResultSet;
76.import java.sql.Statement;
77.import java.util.ArrayList;
78.
79.
80.import com.tianyitime.notebook.support.userPO.UserPO;
81.import com.tianyitime.notebook.support.util.DBTools;
82.
83.
84.
85.
86.public class UserDAO {
87.
88.
89.// 新增user
90.public void saveUserInfo(UserPO upo)
91.{
92.Connection conn = null;
93.Statement state = null;
94.try {
95.conn = DBTools.getConnection();
96.state = conn.createStatement();
97.String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";
98.//System.out.println(sql);
99.state.executeUpdate(sql);
100.
101.
102.} catch (Exception ex) {
103.// TODO Auto-generated catch block
104.ex.printStackTrace();
105.}
106.finally
107.{
108.DBTools.closeState(state);
109.DBTools.closeConn(conn);
110.}
111.}
112.
113.
114.//得到一个数据库中当前Id的最大值
115.private int getMaxId()
116.{
117.Connection conn = null;
118.Statement state = null;
119.ResultSet rs = null;
120.int maxId = 0;
121.try {
122.conn = DBTools.getConnection();
123.state = conn.createStatement();
124.String sql = "select max(id) maxId from notebook_user";
125.rs = state.executeQuery(sql);
126.//从resultset对象中将数据取出
127.if(rs.next())
128.{
129.maxId = rs.getInt("maxId");
130.}
131.} catch (Exception ex) {
132.// TODO Auto-generated catch block
133.ex.printStackTrace();
134.}
135.
136.
137.return ++maxId;
138.}
139.
140.
141.// 得到所有的记录
142.public ArrayList getUserInfo()
143.{
144.Connection conn = null;
145.Statement state = null;
146.ResultSet rs = null;
147.UserPO upo = null;
148.ArrayList al = new ArrayList();
149.try {
150.conn = DBTools.getConnection();
151.state = conn.createStatement();
152.String sql = "select * from notebook_user";
153.rs = state.executeQuery(sql);
154.//从resultset对象中将数据取出
155.
156.
157.while(rs.next())
158.{
159.upo = new UserPO();
160.int id = rs.getInt("id");
161.String yhm = rs.getString("yhm");
162.String email = rs.getString("email");
163.String content = rs.getString("content");
164.
165.
166.upo.setId(id);
167.upo.setYhm(yhm);
168.upo.setEmail(email);
169.upo.setContent(content);
170.
171.
172.//将改对象放入已经创建好的集合类对象ArrauyList
173.al.add(upo);
174.}
175.} catch (Exception ex) {
176.// TODO Auto-generated catch block
177.ex.printStackTrace();
178.}
179.finally
180.{
181.DBTools.closeRs(rs);
182.DBTools.closeState(state);
183.DBTools.closeConn(conn);
184.}
185.return al;
186.}
187.
188.
189.// 删除一条user记录
190.public void deleteUserInfo(int id)
191.{
192.Connection conn = null;
193.Statement state = null;
194.try {
195.conn = DBTools.getConnection();
196.state = conn.createStatement();
197.String sql = "delete from notebook_user where id="+id;
198.//System.out.println(sql);
199.state.executeUpdate(sql);
200.
201.
202.} catch (Exception ex) {
203.// TODO Auto-generated catch block
204.ex.printStackTrace();
205.}
206.finally
207.{
208.DBTools.closeState(state);
209.DBTools.closeConn(conn);
210.}
211.}
212.
213.
214.// 根据给定的信息得到记录
215.public ArrayList getUserInfoByInfo(String name,String email,String content)
216.{
217.Connection conn = null;
218.Statement state = null;
219.ResultSet rs = null;
220.UserPO upo = null;
221.ArrayList al = new ArrayList();
222.try {
223.conn = DBTools.getConnection();
224.state = conn.createStatement();
225.String sql = "select * from notebook_user where 1=1 ";
226.if(!"".equals(name) && name != null)
227.{
228.sql += " and yhm like '%"+name+"%'";
229.}
230.if(!"".equals(email) && email != null)
231.{
232.sql += " and email = '"+email+"'";
233.}
234.if(!"".equals(content) && content != null)
235.{
236.sql += " and content like '%"+content+"%'";
237.}
238.sql+=" order by id desc";
239.rs = state.executeQuery(sql);
240.//从resultset对象中将数据取出
241.
242.
243.while(rs.next())
244.{
245.upo = new UserPO();
246.int id = rs.getInt("id");
247.String yhm = rs.getString("yhm");
248.String femail = rs.getString("email");
249.String fcontent = rs.getString("content");
250.
251.
252.upo.setId(id);
253.upo.setYhm(yhm);
254.upo.setEmail(femail);
255.upo.setContent(fcontent);
256.
257.
258.//将改对象放入已经创建好的集合类对象ArrauyList
259.al.add(upo);
260.}
261.} catch (Exception ex) {
262.// TODO Auto-generated catch block
263.ex.printStackTrace();
264.}
265.finally
266.{
267.DBTools.closeRs(rs);
268.DBTools.closeState(state);
269.DBTools.closeConn(conn);
270.}
271.return al;
272.}
273.
274.
275.}
在这行代码80.import com.tianyitime.notebook.support.userPO.UserPO; 1 中的意识是根据这个包的路径查找到userPO类的Userpo方法?2.那么看着所有代码的意识是我又没有新建这个userPO类以及里面的UserPO方法,那么它是JAVA内部自带的?
01.import java.sql.Connection;
02.import java.sql.DriverManager;
03.import java.sql.ResultSet;
04.import java.sql.SQLException;
05.import java.sql.Statement;
06.
07.
08.public class DBTools {
09.// 定义一个方法,用来得到一个"新的"连接对象
10.public static Connection getConnection()
11.{
12.Connection conn = null;
13.String driverName = "oracle.jdbc.driver.OracleDriver";
14.String url = "jdbc:oracle:thin:@localhost:1521:ora9i";
15.String userName = "scott";
16.String passWord = "tiger";
17.try {
18.Class.forName(driverName);
19.conn = DriverManager.getConnection(url,userName,passWord );
20.} catch (Exception e) {
21.// TODO Auto-generated catch block
22.e.printStackTrace();
23.}
24.return conn;
25.}
26.
27.
28.public static void closeConn(Connection conn)
29.{
30.try {
31.if(conn != null)
32.{
33.conn.close();
34.}
35.} catch (SQLException e) {
36.// TODO Auto-generated catch block
37.e.printStackTrace();
38.}
39.}
40.
41.
42.public static void closeState(Statement state)
43.{
44.try {
45.if(state != null)
46.{
47.state.close();
48.}
49.} catch (SQLException e) {
50.// TODO Auto-generated catch block
51.e.printStackTrace();
52.}
53.}
54.
55.
56.public static void closeRs(ResultSet rs)
57.{
58.try {
59.if(rs != null)
60.{
61.rs.close();
62.}
63.} catch (SQLException e) {
64.// TODO Auto-generated catch block
65.e.printStackTrace();
66.}
67.}
68.}
69.
70.
71.
72.
73.
74.
75.import java.sql.ResultSet;
76.import java.sql.Statement;
77.import java.util.ArrayList;
78.
79.
80.import com.tianyitime.notebook.support.userPO.UserPO;
81.import com.tianyitime.notebook.support.util.DBTools;
82.
83.
84.
85.
86.public class UserDAO {
87.
88.
89.// 新增user
90.public void saveUserInfo(UserPO upo)
91.{
92.Connection conn = null;
93.Statement state = null;
94.try {
95.conn = DBTools.getConnection();
96.state = conn.createStatement();
97.String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";
98.//System.out.println(sql);
99.state.executeUpdate(sql);
100.
101.
102.} catch (Exception ex) {
103.// TODO Auto-generated catch block
104.ex.printStackTrace();
105.}
106.finally
107.{
108.DBTools.closeState(state);
109.DBTools.closeConn(conn);
110.}
111.}
112.
113.
114.//得到一个数据库中当前Id的最大值
115.private int getMaxId()
116.{
117.Connection conn = null;
118.Statement state = null;
119.ResultSet rs = null;
120.int maxId = 0;
121.try {
122.conn = DBTools.getConnection();
123.state = conn.createStatement();
124.String sql = "select max(id) maxId from notebook_user";
125.rs = state.executeQuery(sql);
126.//从resultset对象中将数据取出
127.if(rs.next())
128.{
129.maxId = rs.getInt("maxId");
130.}
131.} catch (Exception ex) {
132.// TODO Auto-generated catch block
133.ex.printStackTrace();
134.}
135.
136.
137.return ++maxId;
138.}
139.
140.
141.// 得到所有的记录
142.public ArrayList getUserInfo()
143.{
144.Connection conn = null;
145.Statement state = null;
146.ResultSet rs = null;
147.UserPO upo = null;
148.ArrayList al = new ArrayList();
149.try {
150.conn = DBTools.getConnection();
151.state = conn.createStatement();
152.String sql = "select * from notebook_user";
153.rs = state.executeQuery(sql);
154.//从resultset对象中将数据取出
155.
156.
157.while(rs.next())
158.{
159.upo = new UserPO();
160.int id = rs.getInt("id");
161.String yhm = rs.getString("yhm");
162.String email = rs.getString("email");
163.String content = rs.getString("content");
164.
165.
166.upo.setId(id);
167.upo.setYhm(yhm);
168.upo.setEmail(email);
169.upo.setContent(content);
170.
171.
172.//将改对象放入已经创建好的集合类对象ArrauyList
173.al.add(upo);
174.}
175.} catch (Exception ex) {
176.// TODO Auto-generated catch block
177.ex.printStackTrace();
178.}
179.finally
180.{
181.DBTools.closeRs(rs);
182.DBTools.closeState(state);
183.DBTools.closeConn(conn);
184.}
185.return al;
186.}
187.
188.
189.// 删除一条user记录
190.public void deleteUserInfo(int id)
191.{
192.Connection conn = null;
193.Statement state = null;
194.try {
195.conn = DBTools.getConnection();
196.state = conn.createStatement();
197.String sql = "delete from notebook_user where id="+id;
198.//System.out.println(sql);
199.state.executeUpdate(sql);
200.
201.
202.} catch (Exception ex) {
203.// TODO Auto-generated catch block
204.ex.printStackTrace();
205.}
206.finally
207.{
208.DBTools.closeState(state);
209.DBTools.closeConn(conn);
210.}
211.}
212.
213.
214.// 根据给定的信息得到记录
215.public ArrayList getUserInfoByInfo(String name,String email,String content)
216.{
217.Connection conn = null;
218.Statement state = null;
219.ResultSet rs = null;
220.UserPO upo = null;
221.ArrayList al = new ArrayList();
222.try {
223.conn = DBTools.getConnection();
224.state = conn.createStatement();
225.String sql = "select * from notebook_user where 1=1 ";
226.if(!"".equals(name) && name != null)
227.{
228.sql += " and yhm like '%"+name+"%'";
229.}
230.if(!"".equals(email) && email != null)
231.{
232.sql += " and email = '"+email+"'";
233.}
234.if(!"".equals(content) && content != null)
235.{
236.sql += " and content like '%"+content+"%'";
237.}
238.sql+=" order by id desc";
239.rs = state.executeQuery(sql);
240.//从resultset对象中将数据取出
241.
242.
243.while(rs.next())
244.{
245.upo = new UserPO();
246.int id = rs.getInt("id");
247.String yhm = rs.getString("yhm");
248.String femail = rs.getString("email");
249.String fcontent = rs.getString("content");
250.
251.
252.upo.setId(id);
253.upo.setYhm(yhm);
254.upo.setEmail(femail);
255.upo.setContent(fcontent);
256.
257.
258.//将改对象放入已经创建好的集合类对象ArrauyList
259.al.add(upo);
260.}
261.} catch (Exception ex) {
262.// TODO Auto-generated catch block
263.ex.printStackTrace();
264.}
265.finally
266.{
267.DBTools.closeRs(rs);
268.DBTools.closeState(state);
269.DBTools.closeConn(conn);
270.}
271.return al;
272.}
273.
274.
275.}
在这行代码80.import com.tianyitime.notebook.support.userPO.UserPO; 1 中的意识是根据这个包的路径查找到userPO类的Userpo方法?2.那么看着所有代码的意识是我又没有新建这个userPO类以及里面的UserPO方法,那么它是JAVA内部自带的?