I
igod
Unregistered / Unconfirmed
GUEST, unregistred user!
数据库中有INT型字段,且唯一,插入数据时要进行增量处理,我的思路是先SELECT MAX(ID) 然后+1得到新的ID号,再插入,可是运行时报告ID未被定义.我可是在SELECT的时候定义了INT ID的呀,该怎么解决请大家帮忙.
程序如下:
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Date"%>
<HTML>
<HEAD>
<TITLE> 添加新闻 </TITLE>
</HEAD>
<BODY>
<%//-------判断权限
String login=(String)session.getAttribute("newsadmin_login");
if(login=="yes")
{
}
else
{response.sendRedirect("news_admin.html");}//-----------
%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:jspdb";
Connection con=DriverManager.getConnection(url,"test","test");
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select max(news_id) from news");
while(result.next())
{
int id=result.getInt("news_id")+1;
}
result.close();
con.close();
}
catch(Exception e)
{out.println(e);}
%>
<%
String title=request.getParameter("title");
String content=request.getParameter("content");
if(title==null||title.trim().compareTo("")==0)
out.println("必须填写新闻标题!");
else
if(content==null||content.trim().compareTo("")==0)
out.println("请填写新闻内容!");
else
{
Date now_time=new Date();
int year=now_time.getYear()+1900;
int month=now_time.getMonth()+1;
int day=now_time.getDate();
int hour=now_time.getHours();
int minute=now_time.getMinutes();
int second=now_time.getSeconds();
String news_time=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:jspdb";
Connection con=DriverManager.getConnection(url,"test","test");
PreparedStatement stm=con.prepareStatement("insert into news values(?,?,?,?)");
stm.setInt(1,id);
stm.setString(2,title);
stm.setString(3,content);
stm.setString(4,news_time);
try{
stm.executeQuery();
out.println("新闻添加成功!<a href=add_news.jsp>继续添加</a>");
}catch(Exception e)
{out.println(e);}
con.close();
//result.close();
}
catch(Exception e)
{out.println(e);}
%>
<%
}
%>
</BODY>
</HTML>
报这个错:91: Undefined variable: id
stm.setInt(1,id);
请帮忙看看到底该如何改,谢谢
程序如下:
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Date"%>
<HTML>
<HEAD>
<TITLE> 添加新闻 </TITLE>
</HEAD>
<BODY>
<%//-------判断权限
String login=(String)session.getAttribute("newsadmin_login");
if(login=="yes")
{
}
else
{response.sendRedirect("news_admin.html");}//-----------
%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:jspdb";
Connection con=DriverManager.getConnection(url,"test","test");
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select max(news_id) from news");
while(result.next())
{
int id=result.getInt("news_id")+1;
}
result.close();
con.close();
}
catch(Exception e)
{out.println(e);}
%>
<%
String title=request.getParameter("title");
String content=request.getParameter("content");
if(title==null||title.trim().compareTo("")==0)
out.println("必须填写新闻标题!");
else
if(content==null||content.trim().compareTo("")==0)
out.println("请填写新闻内容!");
else
{
Date now_time=new Date();
int year=now_time.getYear()+1900;
int month=now_time.getMonth()+1;
int day=now_time.getDate();
int hour=now_time.getHours();
int minute=now_time.getMinutes();
int second=now_time.getSeconds();
String news_time=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:jspdb";
Connection con=DriverManager.getConnection(url,"test","test");
PreparedStatement stm=con.prepareStatement("insert into news values(?,?,?,?)");
stm.setInt(1,id);
stm.setString(2,title);
stm.setString(3,content);
stm.setString(4,news_time);
try{
stm.executeQuery();
out.println("新闻添加成功!<a href=add_news.jsp>继续添加</a>");
}catch(Exception e)
{out.println(e);}
con.close();
//result.close();
}
catch(Exception e)
{out.println(e);}
%>
<%
}
%>
</BODY>
</HTML>
报这个错:91: Undefined variable: id
stm.setInt(1,id);
请帮忙看看到底该如何改,谢谢