You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[topic]' at line 1
10 什么意思
10 什么意思
解决方案 »
- 问:JavaWeb页面超链接启动客户端应用程序
- 怎样用Tomcat-6.0.18呢?
- tomcat
- 新手连接MYSQL5.0数据库问题
- 请问一个关于URL定向的问题
- 项目发布后怎么访问??
- 访问数据库时抛出如下例外,是什么意思?如何解决?
- 我从网上下载了一本名叫javabeandevelop的书 ,可是解压后发现尽是些.001 .002 .003文件,请问我要用什么东西才能看到内容?
- 请问谁有JS或Java的日期控件
- 怎么能打开*.class的文件。最好是可视的!谢谢大家!!
- 标签radio
- 用eclipese开发 ibatis + struts2.0+ oracle 启动tomcat报错,帮忙看下
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.study.dao.DAOFactory;
import com.study.dao.TopicDAO;
import com.study.util.DataContext;
import com.study.vo.Topic;public class saveTopic extends HttpServlet { /**
* Constructor of the object.
*/
public saveTopic() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { boolean re = false;
response.setContentType("text/html");
response.setCharacterEncoding("GB18030");
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
PrintWriter out = response.getWriter();
int topicid = Integer.parseInt(request.getParameter("topicid"));
Topic topic = new Topic();
topic.setTopicid(topicid);
TopicDAO topicdao = DAOFactory.getTopicDAO();
try{
re = topicdao.delTopic(topic);
if(re){
out.println("<script language=javascript>alert('操作成功!');window.location.href='"+basePath+"listtopic.jsp';</script>");
}
}catch(Exception ex){
}
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { boolean re = false;
response.setContentType("text/html");
response.setCharacterEncoding("GB18030");
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
PrintWriter out = response.getWriter();
String type = request.getParameter("type");
String topictitle = request.getParameter("topictitle");
Topic topic = new Topic();
if(topictitle==null||topictitle.length()<1){
out.println("<script language=javascript>window.location.href='../register.jsp?err=topictitle';</script>");
out.flush();
out.close();
}
topictitle = DataContext.getDataContext(topictitle);
topic.setTopicname(topictitle);
TopicDAO topicdao = DAOFactory.getTopicDAO();
if(type.equalsIgnoreCase("add")){
try{
re = topicdao.addTopic(topic);
if(re){
out.println("<script language=javascript>alert('操作成功!');window.location.href='"+basePath+"listtopic.jsp';</script>");
}
}catch(Exception ex){}
}else if(type.equalsIgnoreCase("update")){
try{
int topicid = Integer.parseInt(request.getParameter("topicid"));
topic.setTopicid(topicid);
re = topicdao.updateTopic(topic);
if(re){
out.println("<script language=javascript>alert('操作成功!');window.location.href='"+basePath+"listtopic.jsp';</script>");
}
}catch(Exception ex){}
}
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}}
在第一行
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;import com.study.database.DBConnectionManage;
import com.study.vo.Message;
import com.study.vo.Topic;
import com.study.vo.Member;public class MessageDAOImpl implements MessageDAO { public boolean addMessage(Message instance) {
boolean re = false;
// 根据member表中的字段,拼接SQL语句
String query = "insert into message(messagetitle,messagecontent,messagedate,messagetype,messagememberid,messageparentid,messagetopicid) "
+ "values(?,?,?,?,?,?,?)";
Connection con = null;
PreparedStatement pst = null;
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setString(1, instance.getMessagetitle());
pst.setString(2, instance.getMessagecontent());
pst.setDate(3, instance.getMessagedate());
pst.setInt(4, instance.getMessagetype());
pst.setLong(5, instance.getMessagememberid());
pst.setLong(6, instance.getMessageparentid());
pst.setInt(7, instance.getMessagetopicid());
// 执行sql语句
pst.executeUpdate();
re = true;
} catch (Exception ex) {
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return re = true;
} public boolean delMessage(Message instance) {
boolean re = false;
// 根据member表中的字段,拼接SQL语句
String query = "delete from message where messageid = ?";
Connection con = null;
PreparedStatement pst = null;
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setLong(1, instance.getMessageid());
// 执行sql语句
pst.executeUpdate();
re = true;
} catch (Exception ex) {
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return re = true;
} public Vector liskMessage() {
// TODO Auto-generated method stub
return null;
} public Vector listMessageByMember(Member instance) {
// 根据member表中的字段,拼接SQL语句
String query = "select * from message where messagememberid = ?";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Vector list = new Vector();
Message tmp = null;
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setLong(1, instance.getMemberid());
// 执行sql语句,并将记录集返回给ResultSet
rs = pst.executeQuery();
//循环ResultSet集合,并且封装Message对象
while(rs.next()){
tmp = new Message();
tmp.setMessageid(rs.getLong("messageid"));
tmp.setMessagedate(rs.getDate("Messagedate"));
tmp.setMessagecontent(rs.getString("messagecontent"));
tmp.setMessagememberid(rs.getInt("messagememberid"));
tmp.setMessageparentid(rs.getLong("messageparentid"));
tmp.setMessagetitle(rs.getString("messagetitle"));
tmp.setMessagetopicid(rs.getInt("messagetopicid"));
tmp.setMessagetype(rs.getInt("messagetype"));
list.add(tmp);
tmp = null;
}
} catch (Exception ex) {
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeResult(rs);
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return list;
} public Vector listMessageByMessage(Message instance) {
// 根据member表中的字段,拼接SQL语句
String query = "select * from message m,member mb where m.messagememberid = mb.memberid and m.messageparentid = ? order by m.messagetype";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Vector list = new Vector();
Message tmp = null;
Member membertmp = null;
MemberDAO memberdao = DAOFactory.getMemberDAO();
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setLong(1, instance.getMessageid());
// 执行sql语句,并将记录集返回给ResultSet
rs = pst.executeQuery();
//循环ResultSet集合,并且封装Message对象
while(rs.next()){
tmp = new Message();
tmp.setMessageid(rs.getLong("messageid"));
tmp.setMessagetitle(rs.getString("messagetitle"));
tmp.setMessagecontent(rs.getString("messagecontent"));
tmp.setMessagedate(rs.getDate("Messagedate"));
tmp.setMessagetype(rs.getInt("messagetype"));
membertmp = memberdao.getMemberById(rs.getLong("messagememberid"));
if(membertmp!=null){
tmp.setMember(membertmp);
}
tmp.setMessageparentid(rs.getLong("messageparentid"));
tmp.setMessagetopicid(rs.getInt("messagetopicid"));
list.add(tmp);
tmp = null;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeResult(rs);
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return list;
} public Vector listMessageByTopic(Topic instance) {
// 根据message表中的字段,拼接SQL语句
String query = "select * from message m,member mb where m.messagememberid = mb.memberid and m.messagetopicid = ? and m.messagetype = 0 ";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Vector list = new Vector();
Message tmp = null;
Member membertmp = null;
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
MemberDAO memberdao = DAOFactory.getMemberDAO();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setLong(1, instance.getTopicid());
// 执行sql语句,并将记录集返回给ResultSet
rs = pst.executeQuery();
//循环ResultSet集合,并且封装Message对象
while(rs.next()){
tmp = new Message();
tmp.setMessageid(rs.getLong("messageid"));
tmp.setMessagetitle(rs.getString("messagetitle"));
tmp.setMessagecontent(rs.getString("messagecontent"));
tmp.setMessagedate(rs.getDate("Messagedate"));
tmp.setMessagetype(rs.getInt("messagetype"));
membertmp = memberdao.getMemberById(rs.getLong("messagememberid"));
if(membertmp!=null){
tmp.setMember(membertmp);
}
tmp.setMessageparentid(rs.getLong("messageparentid"));
tmp.setMessagetopicid(rs.getInt("messagetopicid"));
list.add(tmp);
tmp = null;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeResult(rs);
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return list;
}
public Message getMessageById(long messageid) {
// 根据message表中的字段,拼接SQL语句
String query = "select * from message where messageid = ?";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Message instance = null;
Member membertmp = null;
// 获取数据库连接池
DBConnectionManage dbmanage = DBConnectionManage.getInstance();
MemberDAO memberdao = DAOFactory.getMemberDAO();
try {
// 从数据库连接池中获取数据连接
con = dbmanage.getFreeConnection();
pst = con.prepareStatement(query);
pst.setLong(1, messageid);
// 执行sql语句,并将记录集返回给ResultSet
rs = pst.executeQuery();
//循环ResultSet集合,并且封装Message对象
while(rs.next()){
instance = new Message();
instance.setMessageid(rs.getLong("messageid"));
instance.setMessagetitle(rs.getString("messagetitle"));
instance.setMessagecontent(rs.getString("messagecontent"));
instance.setMessagedate(rs.getDate("Messagedate"));
instance.setMessagetype(rs.getInt("messagetype"));
membertmp = memberdao.getMemberById(rs.getLong("messagememberid"));
if(membertmp!=null){
instance.setMember(membertmp);
}
instance.setMessageparentid(rs.getLong("messageparentid"));
instance.setMessagetopicid(rs.getInt("messagetopicid"));
}
} catch (Exception ex) {
} finally {
try {
// 关闭数据库连接,释放资源
dbmanage.closeResult(rs);
dbmanage.closeStatement(pst);
dbmanage.closeConnection(con);
} catch (Exception ex) {
}
}
return instance;
}
public static void main(String args[]){
MessageDAOImpl md = new MessageDAOImpl();
Message m = new Message();
m.setMessageid(1);
Vector v = md.listMessageByMessage(m);
System.out.println(v.size());
}
}