新手问题,大侠请看,谢谢 JAVA 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 说的不清楚,你deubg 看看。 i++,,,++i 我的疑问是125行已经查到了最大的maxId,为什么137是.return ++maxId;而不是return maxId???假如我用的是return ++maxId不是比我最大的ID大一个,请大侠解答。 我疑问的是 最后为什么是返回的是++maxId 而不是maxId? 他这个方法的用意就是查询出最大的ID然后在它上面+1得到新的ID值用来新增数据时手动插入ID值(猜测) 你的猜测很有道理,就是他查询出来最大ID,返回maxId就行了是吧,谢谢你哈 你定义的maxId=0,而数据库中id是从1开始的 但是你看 129.maxId = rs.getInt("maxId"); 这行代码不就是取得最大maxId并且赋值给120.int maxId 但是你看 129.maxId = rs.getInt("maxId"); 这行代码不就是取得最大maxId并且赋值给120.int maxId 是这样的,我在网上搜了一下发现,事实并不是你想的那样// 新增user public void saveUserInfo(UserPO upo) { Connection conn = null; Statement state = null; try { conn = DBTools.getConnection(); state = conn.createStatement(); String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')"; //System.out.println(sql); state.executeUpdate(sql); } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } finally { DBTools.closeState(state); DBTools.closeConn(conn); } } 这里的插入语句是在原有的id的基础上+1而已 但是你看 129.maxId = rs.getInt("maxId"); 这行代码不就是取得最大maxId并且赋值给120.int maxId 是这样的,我在网上搜了一下发现,事实并不是你想的那样// 新增user public void saveUserInfo(UserPO upo) { Connection conn = null; Statement state = null; try { conn = DBTools.getConnection(); state = conn.createStatement(); String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')"; //System.out.println(sql); state.executeUpdate(sql); } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } finally { DBTools.closeState(state); DBTools.closeConn(conn); } } 这里的插入语句是在原有的id的基础上+1而已兄弟 不好意识 我没懂你的意识 请问跟我问的有关系吗? 全部代码奉上我就纳闷 我需要的是查询到最大ID号那么125行已经查到了最大的maxId,为什么137是.return ++maxId;而不是return maxId???请大侠解答 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.} 12楼说的是对的,你数据库中的这张表id是手动设置的,当需要新增一条记录的时候就需要把以前记录中最大的id查出来,然后+1,作为新增记录的id值 dwr调用的方法没执行成功的问题 【急,高分请教】关于Double类型四舍五入的问题 购物车的session问题 欢迎网友访问myoffice办公网,myoffice办公网是您网上办公的最好平台! 调用saybase存储过程的问题 表格边框 怎样只画竖线,不画出横线 请问struts中如何从bean中向servlet返回一个ResultSet? 关于购物网站权限问题 简单问题,快拿分! 再来100分,请教jsp中js问题 jquery中判断为空问题 spring3.0.6 mvc+hibernate3.6.10注解遇到问题
public void saveUserInfo(UserPO upo)
{
Connection conn = null;
Statement state = null;
try {
conn = DBTools.getConnection();
state = conn.createStatement();
String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";
//System.out.println(sql);
state.executeUpdate(sql);
} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
finally
{
DBTools.closeState(state);
DBTools.closeConn(conn);
}
}
这里的插入语句是在原有的id的基础上+1而已
public void saveUserInfo(UserPO upo)
{
Connection conn = null;
Statement state = null;
try {
conn = DBTools.getConnection();
state = conn.createStatement();
String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";
//System.out.println(sql);
state.executeUpdate(sql);
} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
finally
{
DBTools.closeState(state);
DBTools.closeConn(conn);
}
}
这里的插入语句是在原有的id的基础上+1而已兄弟 不好意识 我没懂你的意识 请问跟我问的有关系吗?
我就纳闷 我需要的是查询到最大ID号
那么125行已经查到了最大的maxId,为什么137是.return ++maxId;而不是return maxId???请大侠解答
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.}