Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- public class InfoUpdate {
- private static String url = "jdbc:mysql://localhost:3306/Yourdatabase";
- private static String user = "Yourusername";
- private static String password = "Yourpassword";
- private static Connection conn;
- private static Connection update_conn;
- private static PreparedStatement ps;
- private static PreparedStatement update_ps;
- private static ResultSet rs;
- private static String select_sql;
- private static String update_sql;
- static {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new ExceptionInInitializerError(e);
- }
- }
- public static Connection getConnection() throws SQLException {
- return DriverManager.getConnection(url, user, password);
- }
- public static void free(ResultSet rs, Statement st, Connection conn) {
- try {
- if (rs != null)
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (st != null)
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (conn != null)
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- private static void doUpdate(String kind, String prop, String item_numid) throws SQLException {
- update_conn = getConnection();
- update_sql = "update item_detail set " + kind + " = ? where Item_numID = ?";
- update_ps = update_conn.prepareStatement(update_sql);
- update_ps.setString(1, prop);
- update_ps.setString(2, item_numid);
- update_ps.executeUpdate();
- free(null, update_ps, update_conn);
- }
- private static void print(String Item_Prop, String item_numid) throws SQLException {
- String[] desc = Item_Prop.split(";");
- for (String string : desc) {
- if(string.startsWith("品牌")) {
- String[] prop = string.split(":");
- System.err.println("品牌:" + prop[1]);
- doUpdate("brand", prop[1], item_numid);
- }
- if(string.startsWith("风格")) {
- String[] prop = string.split(":");
- System.err.println("风格:" + prop[1]);
- doUpdate("fashion", prop[1], item_numid);
- }
- }
- }
- private static void getCategory(String Pro_Category, String Sub_Category, String item_numid) throws SQLException {
- int start = Pro_Category.indexOf("/",Pro_Category.indexOf(Sub_Category)) + 1;
- int end_maybe = Pro_Category.indexOf("/", start);
- int end = (end_maybe==-1)?Pro_Category.length():end_maybe;
- // System.err.println(start);
- // System.err.println(end);
- String catag = Pro_Category.substring(start, end);
- doUpdate("catag", catag, item_numid);
- }
- private static void update() throws SQLException {
- conn = getConnection();
- select_sql = "select item_numid,item_prop,sub_category,category from item_detail limit 10;";
- ps = conn.prepareStatement(select_sql);
- rs = ps.executeQuery();
- while(rs.next()) {
- String item_prop = rs.getString("item_Prop");
- String sub_category = rs.getString("Sub_Category");
- String item_numid = rs.getString("item_numid");
- String category = rs.getString("category");
- print(item_prop, item_numid);
- getCategory(sub_category, category, item_numid);
- }
- free(rs, ps, conn);
- }
- public static void main(String[] args) throws SQLException {
- Date date1 = new Date();
- update();
- Date date2 = new Date();
- System.err.println(date2.getTime() - date1.getTime() + "ms");
- }
- }
Add Comment
Please, Sign In to add comment