我先发表一段代码,有一段看的不是很懂,
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.