Guest User

Untitled

a guest
Jun 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import java.util.Date;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8.  
  9. public class InfoUpdate {
  10. private static String url = "jdbc:mysql://localhost:3306/Yourdatabase";
  11. private static String user = "Yourusername";
  12. private static String password = "Yourpassword";
  13. private static Connection conn;
  14. private static Connection update_conn;
  15. private static PreparedStatement ps;
  16. private static PreparedStatement update_ps;
  17. private static ResultSet rs;
  18. private static String select_sql;
  19. private static String update_sql;
  20. static {
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. } catch (ClassNotFoundException e) {
  24. throw new ExceptionInInitializerError(e);
  25. }
  26. }
  27.  
  28. public static Connection getConnection() throws SQLException {
  29. return DriverManager.getConnection(url, user, password);
  30. }
  31.  
  32. public static void free(ResultSet rs, Statement st, Connection conn) {
  33. try {
  34. if (rs != null)
  35. rs.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. if (st != null)
  41. st.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. } finally {
  45. if (conn != null)
  46. try {
  47. conn.close();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. }
  54. private static void doUpdate(String kind, String prop, String item_numid) throws SQLException {
  55. update_conn = getConnection();
  56. update_sql = "update item_detail set " + kind + " = ? where Item_numID = ?";
  57. update_ps = update_conn.prepareStatement(update_sql);
  58. update_ps.setString(1, prop);
  59. update_ps.setString(2, item_numid);
  60. update_ps.executeUpdate();
  61. free(null, update_ps, update_conn);
  62. }
  63.  
  64. private static void print(String Item_Prop, String item_numid) throws SQLException {
  65. String[] desc = Item_Prop.split(";");
  66. for (String string : desc) {
  67. if(string.startsWith("品牌")) {
  68. String[] prop = string.split(":");
  69. System.err.println("品牌:" + prop[1]);
  70. doUpdate("brand", prop[1], item_numid);
  71. }
  72. if(string.startsWith("风格")) {
  73. String[] prop = string.split(":");
  74. System.err.println("风格:" + prop[1]);
  75. doUpdate("fashion", prop[1], item_numid);
  76. }
  77. }
  78. }
  79.  
  80. private static void getCategory(String Pro_Category, String Sub_Category, String item_numid) throws SQLException {
  81. int start = Pro_Category.indexOf("/",Pro_Category.indexOf(Sub_Category)) + 1;
  82. int end_maybe = Pro_Category.indexOf("/", start);
  83. int end = (end_maybe==-1)?Pro_Category.length():end_maybe;
  84. // System.err.println(start);
  85. // System.err.println(end);
  86. String catag = Pro_Category.substring(start, end);
  87. doUpdate("catag", catag, item_numid);
  88. }
  89.  
  90. private static void update() throws SQLException {
  91. conn = getConnection();
  92. select_sql = "select item_numid,item_prop,sub_category,category from item_detail limit 10;";
  93. ps = conn.prepareStatement(select_sql);
  94. rs = ps.executeQuery();
  95. while(rs.next()) {
  96. String item_prop = rs.getString("item_Prop");
  97. String sub_category = rs.getString("Sub_Category");
  98. String item_numid = rs.getString("item_numid");
  99. String category = rs.getString("category");
  100. print(item_prop, item_numid);
  101. getCategory(sub_category, category, item_numid);
  102. }
  103. free(rs, ps, conn);
  104. }
  105.  
  106. public static void main(String[] args) throws SQLException {
  107. Date date1 = new Date();
  108. update();
  109. Date date2 = new Date();
  110. System.err.println(date2.getTime() - date1.getTime() + "ms");
  111. }
  112. }
Add Comment
Please, Sign In to add comment