This article talks about simultaneously crawling rss news text from multiple sites, and then Design and implementation of importing into discuz forum
Intercepting news text, please see the first two sections
http://www.voidcn.com/article/p-ovaqymau-bky.html
http://www.voidcn.com/article/p-uusyiehl-bky.html
There are many discuz forums now, and many people use this. Sometimes there is a need to automatically pour news into the forum instead of manually posting, then how to realize this automatic posting? . In fact, it is to manipulate its database, discuz hundreds of tables are difficult to find, but some people have found it. Now I have implemented the function of importing with java code. I will talk to you here and it will be a summary.
Posting involves Four tables
forum_post(post information statistics),< /span>
forum_thread(theme information table span>),
forum_post_tableid(),
forum_forum(section information table),
Insert the found data into these four tables. This involves transaction processing, either at the same time inserting successfully, or All failed
It seems that my DBdao is not very complicated. If it can handle this operation, it’s ok
< p>
mysql configuration file
driver=com.mysql.jdbc.Driverurl=jdbc:mysql ://192.168.2.150:3306/home?useUnicode=true&characterEncoding=GBKusername=anyonepassword=anyone
< /span>
import com.liux.util. PropertiesUtil;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.Properties;public class DBUtil {// Create static global variable static Connection conn ; private static DBUtil dbUtil; static PreparedStatement st; private static String driver; private static String url; // username to connect to the database private static String username; // password to connect to the database private static String password; static {PropertiesUtil pu = PropertiesUtil.getInstance() ; Properties p = pu.getProerties(); driver = p.getProperty("driver").trim(); url = p.getProperty("url").trim(); username = p.getProperty("username") .trim(); password = p.getProperty("password").trim();} /* function to get the database connection*/ public static Connection getConnection() {Connection con = null; //Create a connection to the database Connection object try {Class.forName(driver);// Load Mysql data driver con = DriverManager.getConnection(url, username, password);// Create data connection} catch (Exception e) {System.out.println("Database Connection failed" + e.getMessage());} return con; //Return to the database connection established} /** * Start transaction* * @param cnn */ public static void begin Transaction(Connection cnn) {if (cnn != null) {try {if (cnn.getAutoCommit()) {cnn.setAutoCommit(false);}} catch (SQLException e) {e.printStackTrace();}}} / ** * Commit transaction* * @param cnn */ public static void commitTransaction(Connection cnn) {if (cnn != null) {try {if (!cnn.getAutoCommit()) {cnn.commit();}} catch (SQLException e) {e.printStackTrace();}}} /** * Rollback transaction* * @param cnn */ public static void rollBackTransaction(Connection cnn) {if (cnn != null) {try {if (! cnn.getAutoCommit()) {cnn.rollback();}} catch (SQLException e) {e.printStackTrace();}} }}
Start insert operation:
package com.liux.db;import com.liux.bean.PreForumPost;import com.liux.bean.RSSItemBean;import java.sql.Connection;import java.sql.PreparedStatement;import java. sql.ResultSet;import java.sql.SQLException;/** * Created with IntelliJ IDEA. * User: liuxing * Date: 13-11-5 * Time: 4:41 PM * To change this template use File | Settings | File Templates. */public class RssDao {/* Insert data records and output the number of inserted data records*/ public void insert(RSSItemBean rss) {int lastPid = getLastPostPid(); int lastTid = getLastPostTid(); Connection conn = DBUtil. getConnection(); // First get the connection, that is, connect to the database int fid = rss.getFid(); int time = 0; if (!checkExist(rss.getTitle())) {//Check whether the content has been captured Int currentPId = lastPid; int currentTid = lastTid; PreparedStatement st; try {DBUtil.beginTransaction(conn); //open Things//values(15,36,13,1,'Hurricane Snail',3,'aa',1383553857,'bb','1270.0.1',2998,1,-1,0,1); String sql = "INSERT INTO pre_forum_post(pid, fid, tid,first, author,authorid,subject,dateline,message,useip,port,usesig,htmlon,smileyoff,attachment,position) values(?,?,?,?,?, ?,?,?,?,?,?,?,?,?,?,?)"; st = (PreparedStatement) conn.prepareStatement(sql); // Create a Statement object st. setInt(1, ++currentPId); // pid st.setInt(2, fid); // fid st.setInt(3, ++currentTid); // tid st.setBoolean(4, true); // first st.setString(5, "Hurricaneous Snail"); // author st.setInt(6, 3); // authorid st.setString(7, rss.getTitle()); // subject time = (int) (System .currentTimeMillis() / 1000); st.setInt(8, time); // dateline st.setS tring(9, rss.getContent()); // message st.setString(10, "127.0.0.1"); // useip st.setInt(11, 2998); // port st.setBoolean(12, true) ; // usesig st.setBoolean(13, true); //htmlon st.setBoolean(14, false); // smileyoff st.setBoolean(15, false); // attachment st.setInt(16, 1); / / position st.executeUpdate(); //Insert the second table String sql2 = "INSERT INTO pre_forum_thread (`fid`, `posttableid`, `typeid`, `sortid`, `readperm`, `price`, `author` , `authorid`, `subject`, `dateline`, `lastpost`, `lastposter`, `views`, `replies`, `displayorder`, `highlight`, `digest`, `rate`, `special`,` attachment`, `moderated`, `closed`, `stickreply`, `recommends`,`recommend_add`, `recommend_s ub`, `heats`, `status`, `isgroup`, `favtimes`,`sharetimes`, `stamp`, `icon`, `pushedaid`, `cover`, `replycredit`) VALUES " + "( ?, 0, 0, 0, 0, 0,'Hurricane Snail', 3, ?,?, ?,'Hurricane Snail', 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,32, 0, 0, 0, -1, -1, 0, 0, 0)"; st = (PreparedStatement) conn.prepareStatement(sql2); //st.setInt( 1,currentPId); st.setInt(1, fid); st.setString(2, rss.getTitle()); //subject st.setInt(3, time); st.setInt(4, time); st. executeUpdate(); //Insert the third table String sql3 = "INSERT INTO pre_forum_post_tableid(`pid`) VALUES (" + currentPId + ")"; st = (PreparedStatement) conn.prepareStatement(sql3); st.executeUpdate() ; //Insert the fourth table//First, look for the maximum pid of forum_post (forum_post_tableid is the same as his pid), the maximum tid of forum_thread, and the initial id+1 as the new id. String sql4 = "UPDATE `pre_forum_forum` SET threads=threads+1, posts=posts+1,todayposts=todayposts+1 ,lastpost='" + currentPId + "" + rss.getTitle() + "" + time + "Hurricane Snail" + "'WHERE fid=" + fid; st = (PreparedStatement) conn.prepareStatement(sql4); st.executeUpdate(); DBUtil.commitTransaction(conn);} catch (Exception e) {e.printStackTrace(); DBUtil.rollBackTransaction(conn);} try {conn.close();} catch (SQLException e) {e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.} System.out. println("success add subject:" + rss.getTitle());} else {System.out.println("this subject is exist :" + rss.getTitle());}} public int getLastPostPid() {String sql = "select pid from pre_forum_post order by pid desc limit 0,1"; Connection c onn = DBUtil.getConnection(); PreForumPost pf = null; try {PreparedStatement prest = conn.prepareStatement(sql); ResultSet rs = prest.executeQuery(); //Listlist = new ArrayList (); while (rs.next()) {return rs.getInt(1);} conn.close();} catch (SQLException e) {e.printStackTrace();} return 0;} public int getLastPostTid() {String sql = "select tid from pre_forum_post order by tid desc limit 0,1"; Connection conn = DBUtil.getConnection(); PreForumPost pf = null; try {PreparedStatement prest = conn.prepareStatement(sql); ResultSet rs = prest.executeQuery() ; //List list = new ArrayList (); while (rs.next()) {return rs.getInt(1);} conn.close();} catch (SQLException e) { e.printStackTrace();} return 0;} /** * Determine whether the content has been captured according to the title* * @param title */ public Boolean checkExist(String title) {String sql = "select * from pre_forum_post p where p. subject = ?"; Connection conn = DBUtil.getConnection(); PreForumPost pf = null; try {PreparedStatement prest = conn.prepareStatement(sql); prest.setString(1, title); ResultSet rs = prest.executeQuery(); if (rs.next()) {return true;} conn.close();} catch (SQLException e) {e.printStackTrace();} return false; }}
The complete download address of the project: http://download .csdn.net/detail/a442180673/6523263
just run the main function directly, or it can be run in tomcat, there is Any questions can come to me