| 发表于:2007-04-13 11:41:50 楼主 |
news.jsp 页: <%@ page contenttype= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorpage= " " %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en " "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd "> <html xmlns= "http://www.w3.org/1999/xhtml "> <head> <meta http-equiv= "content-type " content= "text/html; charset=gb2312 " /> <link rel= "stylesheet " type= "text/css " href= "style.css "/> <title> 添加新闻 </title> </head> <body> <form action= "addnews.jsp " method= "post "> <table width= "800 " border= "0 "> <tr> <td> </td> <td> </td> <td width= "238 "> "*"带有这个符号的为必添项 </td> <td width= "48 "> </td> <td width= "388 "> </td> </tr> <tr> <td width= "68 "> </td> <td width= "36 "> *标题: </td> <td> <input name= "newstitle " type= "text " id= "newstitle " size= "30 " /> </td> <td> 类 型: </td> <td width= "388 "> <select name= "newslx " id= "newslx "> <option selected= "selected " value= "0 "> 选择新闻类型 </option> <option value= "1 "> 热点新闻 </option> <option value= "2 "> 行业新闻 </option> </select> </td> </tr> <tr> <td> </td> <td> *作者: </td> <td width= "238 "> <input name= "newsauthor " type= "text " id= "newsauthor " /> </td> <td width= "48 "> 时 间: </td> <td width= "388 "> <input name= "newstime " type= "text " id= "time " onclick= "getdatestring(this,ocalendarchs) " value= "点击输入时间 " readonly> </td> </tr> <tr> <td> </td> <td> *来源: </td> <td width= "238 "> <input name= "newscfrom " type= "text " id= "newscfrom " /> </td> <td width= "48 "> *关键字: </td> <td width= "388 "> <input name= "newskeyword " type= "text " id= "newskeyword " /> </td> </tr> <tr> <td> </td> <td> *内容: </td> <td colspan= "3 "> <textarea name= "newscontent " cols= "60 " rows= "15 " id= "newscontent "> </textarea> </td> </tr> <tr> <td> </td> <td> </td> <td colspan= "3 "> <input name= " " type= "submit " /> <input name= " " type= "reset " /> </td> </tr> </table> </form> </body> </html> addnews.jsp 页: <%@ page contenttype= "text/html; charset=gb2312 " language= "java " errorpage= " "%> <jsp:usebean id= "conn " scope= "page " class= "news.conn " /> <html> <% string title = request.getparameter( "newstitle "); string lx = request.getparameter( "newslx "); string author = request.getparameter( "newsauthor "); string newstime = request.getparameter( "newstime "); string cfrom = request.getparameter( "newscfrom "); string keyword = request.getparameter( "newskeyword "); string content = request.getparameter( "newscontent "); string sql= "insert into news(title,type,author,time,cfrom,keyword,content) values( "+title+ ", ' "+lx+ " ', ' "+author+ " ', ' "+newstime+ " ', ' "+cfrom+ " ', ' "+keyword+ " ', ' "+content+ " ') "; int ret=0; ret=conn.EXECuteupdate(sql); if (ret!=0){ out.println( " <script language= 'javascript '> alert( '成功! ');window.location.href= 'index.html '; </script> "); }else{ out.println( " <script language= 'javascript '> alert( '失败! ');window.location.href= 'index.html '; </script> "); } %> <head> <title> 添加新闻 </title> </head> <body> </body> </html> conn.java package news; import java.sql.*; public class conn { string sdbdriver = "com.mysql.jdbc.driver "; string sconnstr = "jdbc:mysql://localhost:3306/news "; connection connect = null; resultset rs = null; public conn(){ try{ class.forname(sdbdriver); } catch(java.lang.classnotfoundexception e){ system.err.print(e.getmessage()); } } public resultset EXECutequery(string sql){ try{ connect = drivermanager.getconnection(sconnstr, "root ", "lzyboy "); statement stmt = connect.createstatement(); rs = stmt.EXECutequery(sql); } catch(sqlexception ex){ system.err.print( ex.getmessage()); } return rs; } public int EXECuteinsert(string sql){ int result = 0; try{ connect = drivermanager.getconnection(sconnstr, "root ", "lzyboy "); statement stmt = connect.createstatement(); result = stmt.EXECuteupdate(sql); } catch(sqlexception ex){ system.err.print(ex.getmessage()); } return result; } public int EXECuteupdate(string sql){ int result = 0; try{ connect = drivermanager.getconnection(sconnstr, "root ", "lzyboy "); statement stmt = connect.createstatement(); result = stmt.EXECuteupdate(sql); } catch(sqlexception ex){ system.err.print(ex.getmessage()); } return result; } public void close(){ if(connect!=null){ try{ connect.close(); connect = null; } catch(sqlexception ex){ system.err.print(ex.getmessage()); } } } public static string tochinese(string strvalue){ try{ if(strvalue == null){ return null; } else{ strvalue = new string(strvalue.getbytes( "iso8859_1 "), "gbk "); return strvalue; } } catch(exception ex){ return null; } } } |
|
|
|
|