谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢??ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close();
HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close();
HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); 你stmt都没初始化。。
Statement stmt = null; ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); stmt没实例化,肯定空指针异常
谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢??ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close();
HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+ id ); int num=0; if(rs.next()) num=rs.getInt("count"); num++; System.out.println(num); rs.close(); conn.setAutoCommit(false); stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id ); stmt.close(); conn.commit();
}这个id是从.jsp文件中获取的 这是.jsp文件中的相关部分 ResultSet rs=stmt.executeQuery("select * from vote2"); while(rs.next()) { out.println("<tr>"); out.println("<td><input type='radio'name='id' value='"+rs.getString("id")+"'>"); out.println(rs.getString("item")); out.println("</td>"); } rs.close(); stmt.close(); session.setMaxInactiveInterval(-1); 我定义的每一个单选按钮对应一个name,name=“id”,在.java文件中用 String id=request.getParameter("id"); ,想获取jsp页面中的id的值,来对i相应id对应的投票数目进行更新,stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id ); 但是数据库一直没有改变,如果把id直接换成对应的数字,如id='1',就可以更新,这是为什么??
String id=request.getParameter("id");
谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢??ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
rs.close();
stmt.executeUpdate("update vote set count="+ num +" where id="+id);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return "OK";
package laogen.action;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import laogen.common.DBcon; //DBcon类用来连接数据库,可以连接import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")
public class vote2 extends ActionSupport{
public String execute() throws Exception {
Connection conn = DBcon.getConnection();// 这是用来连通数据库
Statement stmt = null;
try {
HttpServletRequest request = ServletActionContext.getRequest();
String id=request.getParameter("id");
ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
rs.close();
stmt.executeUpdate("update vote set count="+ num +" where id="+id);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return "OK";
}
}
如果是,事物需要显示提交conn.commit();
没有设置过AutoCommit,默认是false吗?这个在sql中怎么修改?
Statement stmt = null; try {
HttpServletRequest request = ServletActionContext.getRequest();
String id=request.getParameter("id");
ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
rs.close();
stmt.executeUpdate("update vote set count="+ num +" where id="+id);
你stmt都没初始化。。
ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
stmt没实例化,肯定空指针异常
谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢??ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
rs.close();
stmt.executeUpdate("update vote set count="+ num +" where id="+id);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return "OK";
参考8楼
stmt = conn.createStatement();
stmt.executeUpdate("update vote set count="+ num +" where id="+id);
谢谢~~上面的问题已经解决,加上了
conn.setAutoCommit(false);
中间是更新的代码;
conn.commit();也行~
ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
stmt没实例化,肯定空指针异常
确实确实加上了stmt = conn.createStatement();
这是vote2.java中的try
try
{
HttpServletRequest request = ServletActionContext.getRequest();
String id=request.getParameter("id");
ResultSet rs=stmt.executeQuery("select * from vote2 where id="+ id );
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
System.out.println(num);
rs.close();
conn.setAutoCommit(false);
stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id );
stmt.close();
conn.commit();
}这个id是从.jsp文件中获取的
这是.jsp文件中的相关部分
ResultSet rs=stmt.executeQuery("select * from vote2");
while(rs.next())
{
out.println("<tr>");
out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>");
out.println(rs.getString("item"));
out.println("</td>");
}
rs.close();
stmt.close();
session.setMaxInactiveInterval(-1);
我定义的每一个单选按钮对应一个name,name=“id”,在.java文件中用 String id=request.getParameter("id"); ,想获取jsp页面中的id的值,来对i相应id对应的投票数目进行更新,stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id );
但是数据库一直没有改变,如果把id直接换成对应的数字,如id='1',就可以更新,这是为什么??
Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/zsn;user=sa;password=zsn";
Connection DBconn=java.sql.DriverManager.getConnection(dbUrl);
Statement stmt = DBconn.createStatement();
ResultSet rs=stmt.executeQuery("select * from vote2");
while(rs.next())
{
out.println("<tr>");
out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>");
out.println(rs.getString("item"));
out.println("</td>");
}
rs.close();
stmt.close();
session.setMaxInactiveInterval(-1);
%> <tr><td align="center">
<s:submit value="投票" theme="simple"></s:submit>
</tr>
</s:form>struts.xml中:
<action name="vote2" class="laogen.action.vote2">
<result name="OK">/success.jsp</result>