java 爬虫项目 有用到(Spider_MeiTuan)
package com.spider.selenium; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.transaction.Synchronization; import com.spider.config.DataIndex; import com.spider.entity.Data; import com.spider.util.DateUtil; public class DataUtil { private static DbUtil dbUtil = new DbUtil(); private static DataIndex dataIndex = new DataIndex() ; public static synchronized void Insert(Data data) throws Exception { Connection con=null; con=dbUtil.getCon(); //调用 DBconnection 类的 conn() 方法连接数据库 PreparedStatement pstmt=null; String sql = "INSERT INTO t_data (address,create_date_time,name,pingfen,renjun,ping) VALUES(?,?,?,?,?,?)"; //插入sql语句 try { pstmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); pstmt.setString(1, data.getAddress()); pstmt.setString(2, DateUtil.formatDate(data.getCreateDateTime(), "yyyy-MM-dd HH:mm") ); pstmt.setString(3, data.getName()); pstmt.setString(4, data.getPingfen()); pstmt.setString(5, data.getRenjun()); pstmt.setString(6, data.getPing()); pstmt.executeUpdate(); //执行sql语句 ResultSet rs=pstmt.getGeneratedKeys(); while (rs.next()) { int id=rs.getInt(1);//返回指定列的值 System.out.println("自增id="+id); } System.out.println("插入成功(* ̄︶ ̄)"); //添加索引 dataIndex.addIndex(data); //添加索引 } catch (SQLException e) { e.printStackTrace(); }finally { dbUtil.closeCon(con); } } }
站长微信:xiaomao0055
站长QQ:14496453