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.14 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class FashionCount {
  9. private static String url = "jdbc:mysql://localhost:3306/ItemsInfo2";
  10. private static String user = "root";
  11. private static String password = "2027";
  12. private static Connection conn;
  13. private static Connection update_conn;
  14. private static Connection insert_conn;
  15. private static Connection select_conn;
  16. private static PreparedStatement ps;
  17. private static PreparedStatement update_ps;
  18. private static PreparedStatement insert_ps;
  19. private static PreparedStatement select_ps;
  20. private static ResultSet rs;
  21. private static ResultSet select_rs;
  22. private static String sql;
  23. private static String update_sql;
  24. private static String insert_sql;
  25. private static String select_sql;
  26. static {
  27. try {
  28. Class.forName("com.mysql.jdbc.Driver");
  29. } catch (ClassNotFoundException e) {
  30. throw new ExceptionInInitializerError(e);
  31. }
  32. }
  33. public static Connection getConnection() throws SQLException {
  34. return DriverManager.getConnection(url, user, password);
  35. }
  36. public static void free(ResultSet rs, Statement st, Connection conn) {
  37. try {
  38. if (rs != null)
  39. rs.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. } finally {
  43. try {
  44. if (st != null)
  45. st.close();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. } finally {
  49. if (conn != null)
  50. try {
  51. conn.close();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. }
  58. private static void insert(String Prop) throws SQLException {
  59. insert_conn = getConnection();
  60. insert_sql = "insert into fashion_count set Fashion = ? , Num = 1";
  61. insert_ps = conn.prepareStatement(insert_sql);
  62. insert_ps.setString(1, Prop);
  63. insert_ps.execute();
  64. free(null, insert_ps, insert_conn);
  65. }
  66. private static void update(String Prop) throws SQLException {
  67. update_conn = getConnection();
  68. update_sql = "update fashion_count set num = num+1 where fashion = ?";
  69. update_ps = update_conn.prepareStatement(update_sql);
  70. update_ps.setString(1, Prop);
  71. update_ps.executeUpdate();
  72. free(null, update_ps, update_conn);
  73. }
  74. private static void generate() throws SQLException {
  75. conn = getConnection();
  76. sql = "select fashion from item_detail;";
  77. ps = conn.prepareStatement(sql);
  78. rs = ps.executeQuery();
  79. while(rs.next()) {
  80. String fashion = rs.getString("fashion");
  81. System.err.println(fashion);
  82. if((fashion==null) || fashion.equals(""))
  83. continue;
  84. fashion = fashion.trim();
  85. select_conn = getConnection();
  86. select_sql = "select fashion from fashion_count where fashion = ?;";
  87. select_ps = conn.prepareStatement(select_sql);
  88. select_ps.setString(1, fashion);
  89. select_rs = select_ps.executeQuery();
  90. if(select_rs.next()) {
  91. System.err.println("try updating");
  92. update(fashion);
  93. }
  94. else {
  95. System.err.println("try inserting");
  96. insert(fashion);
  97. }
  98. free(select_rs, select_ps, select_conn);
  99. }
  100. free(rs, ps, conn);
  101. }
  102. public static void main(String[] args) throws SQLException {
  103. generate();
  104. }
  105. }
Add Comment
Please, Sign In to add comment