In fact, this is a very simple technology that is very common in the news website. The principle is to save the data as a hard disk file, directly through the URL, used to alleviate the pressure of database access. Databases and tables:
Mysql> USE Mysecondnews; Database ChangeDmySql> Desc News; -------- ------------- ---- ----- ------- ---------------- | Field | Type | Null | Key | Default | Extra | -------- - -------------- ------ --- -------- --------- ------- | ID | INT (10) | | Pri | Null | Auto_INCREMENT || Title | VARCHAR (240) | | | | || Path | VARCHAR (240) | | | || Text | | | | | | Addtime | TimeStamp (14) | YES | | Null | | -------- -------------- --- - ----- --------- ----------------
Database.java: / / Define Database
Package org.eleaf.firstnews.Database;
import java.sql.ResultSet; public interface Database {String DATABASE = "mysecondnews"; String USERNAME = "bitan"; String PASSWORD = "bitan"; String URL_PREFIX = "jdbc: mysql: // localhost /" DATABASE; String DRIVER = "Org.gjt.mm.mysql.driver";
INT Doupdate (String SQL); ResultSet Doselect (String SQL);
TouchDatabase.java: / / Customize the JDBC interface class. For simplicity, the Close () method is not processed.
package org.eleaf.firstnews.database; import java.sql *;. public class TouchDatabase implements Database {private Connection conn; private Statement stmt; private PreparedStatement prepstmt; private void init () throws SQLException, ClassNotFoundException {stmt = null; prepstmt = null; Class.forName (DRIVER); conn = DriverManager.getConnection (uRL_PREFIX, USERNAME, PASSWORD);} / ** * Constructor for Statement * * / public TouchDatabase () {try {this.init ();. stmt = conn .createStatement ();} catch (SQLException sqle) {sqle.printStackTrace ();} catch (ClassNotFoundException cnfe) {cnfe.printStackTrace ();.}} / ** * Constructor for PreparedStatement * @param sql Pre-compile SQL string . * / Public TouchDatabase (String SQL) {Try {this.init (); prepstmt = conn.prepar eStatement (sql);} catch (SQLException sqle) {sqle.printStackTrace ();} catch (ClassNotFoundException cnfe) {cnfe.printStackTrace ();}} public int doUpdate (String sql) {// TODO method of automatically generating stubs try { return stmt.executeUpdate (sql);} catch (SQLException sqle) {sqle.printStackTrace (); return 0;}} public ResultSet doSelect (String sql) {// TODO method of automatically generating stubs try {return stmt.executeQuery (sql) } Catch (sqlexception sqle) {sqle.printStackTrace (); return null;} catch (exception e) {E.PrintStackTrace (); return null;
}} Public void setField (int index, String field) {try {prepstmt.setString (index, field);} catch (SQLException sqle) {sqle.printStackTrace ();}} public ResultSet doPrepareSelect () {try {return prepstmt. ExecuteQuery ();} catch (sqlexception sqle) {sqle.printStackTrace (); return null;} catch (exception e) {E.PrintStackTrace (); return null;}} }.com.java: / / News structure Package testnews; public class news {private string id; private string title; private string content; / ** * @return Returns AddTime. * / Public string getaddtime () {return addtime;} / ** * @Param addtime To set the Addtime. * / Public void setaddtime (string addtime) {this.addtime = addtime;} / ** * @return returns Content. * / Public string getContent () {return content;} / ** * @Param Content To set the content. * / Public void setContent (string content) {this.content = content;} / ** * @return Returns ID. * / Public string getId () {return;} / ** * @Param ID To set the ID. * / Public void setid (string id) {this.id = id;} / ** * @return returns PATH. * / Public string getpath () {return path;} / ** * @Param Path To set the path. * / Public void setpath (string path) {this.path = path;} / ** * @return Returns Title. * / Public string gettitle () {return title;} / ** * @Param Title To set the Title. * / Public void settitle (string title) {this.title = title;}}
Donnews.java: // Take the Database for the JDBC Interface Class;
import java.util *;. import java.sql *;. import org.eleaf.firstnews.database *;. public class DoneNews {public Vector getAllNews () {Vector vNews = new Vector (); String sql = "SELECT * FROM News "; TouchDatabase Tdata = new TouchDatabase (); ResultSet RS = TDATA.DOSET RS = TDATA.DOSET RS = TDATA.DOSET RS = TDATA.DOSET (SQL); Try {While (rs.next ()) {news news = new news (); news.setid (" id) ")); News.settitle (RS.GetString (" Title ")); news.setpath (rs.getstring (" path ")); news.setcontent (Rs.getstring (" Content ")); news.setaddtime rs.getString ( "addtime")); vNews.addElement (news);}} catch (SQLException sqle) {sqle.printStackTrace ();} return vNews;} public boolean addNews (News news) {boolean success = false; if (news! = null) {string SQL = "Insert News (Title, Path, Content) VALUES ('" news.gettitle () "','" news.getPath () "" "" "" "; touchdatabase tdata = new touchDatabase (); success = (0! = tdata.doupdate (SQL) }}}}}}}}} WriteNews.java: // Write data flows to the hard disk. And call the donnews.addnews () method. Package testnews;
Import java.io. *; import java.util. *; public class whitNews {private factory string middle_dir = ""; // can be customized to the directory. Private final string file_suffix = ".html"; // can customize the file suffix name. public boolean write (News news, String realPathPrefix, Calendar calendar) {int year = calendar.get (Calendar.YEAR); int month = calendar.get (Calendar.MONTH) 1; int dayOfMonth = calendar.get (Calendar.DAY_OF_MONTH ); String fileName = calendar.getTimeInMillis () FILE_SUFFIX; String realPath = realPathPrefix MIDDLE_DIR year "//" month "//" dayOfMonth "//"; try {if (mkDirs (realPath!) ) {// throw new IOException ( "make dir ( '" realPath "') unsuccessfully!");} FileOutputStream fos = new FileOutputStream (new File (realPath fileName)); byte [] conBytes = news.getContent ( ); fos.write (); fos.close ();} catch (ooexception ooe) {oE.PrintStackTrace (); returnaf false;} String newspath = year "/" month "/" Dayofmonth "/" filename; news.setpath (newspath); Return new donnews (). AddNews (news); String realpath = false; file path = new file (realpath); if (! Path.exists ()) {success = path.mkdirs );}} Form.jsp: // Enter a form
<% @ page language = "java"%>