Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.CharBuffer;
  3. import java.nio.charset.Charset;
  4. import java.sql.*;
  5. import java.util.*;
  6.  
  7. public class WordPressDataBaseFixer {
  8.     private static Connection connect;
  9.     private static Statement statement;
  10.     private static ResultSet resultSet;
  11.     private static PreparedStatement preparedStatement;
  12.  
  13.  
  14.     public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
  15.         Class.forName("com.mysql.jdbc.Driver");
  16.         // Setup the connection with the DB
  17.         connect = DriverManager
  18.                 .getConnection("jdbc:mysql://localhost/wordpress?"
  19.                         + "user=root&password=develop");
  20.         statement = connect.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
  21.         // Result set get the result of the SQL query
  22. /*
  23.         resultSet = statement.executeQuery("show tables");
  24. //        writeResultSet(resultSet);
  25.  
  26.         Map<String, List<String>> tables = new HashMap<String, List<String>>();
  27.         while (resultSet.next()) {
  28.             tables.put(resultSet.getString(1), new LinkedList<String>());
  29.         }
  30.  
  31. //        System.out.println("tables = " + tables);
  32.  
  33.         for (String table : tables.keySet()) {
  34.             resultSet = statement.executeQuery("show columns from " + table);
  35.             while (resultSet.next()) {
  36.                 tables.get(table).add(resultSet.getString(1));
  37.             }
  38. //            System.out.println("table = " + table);
  39. //            System.out.println("tables.get(table) = " + tables.get(table));
  40.         }
  41. */
  42.  
  43.         updateTable("wp_options", Arrays.asList(3,4));
  44.         updateTable("wp_posts", Arrays.asList(5, 6, 7, 12, 17));
  45.  
  46.         connect.close();
  47.     }
  48.  
  49.     private static boolean updateTable(String tableName, List<Integer> columns) throws SQLException {
  50.         Charset utf8charset = Charset.forName("UTF-8");
  51.         Charset windows1258charset = Charset.forName("windows-1258");
  52.  
  53.  
  54.         String query = String.format("select * from " + tableName/*, column, table*/);
  55.         resultSet = statement.executeQuery(query);
  56.         while (resultSet.next()) {
  57.             try {
  58.                 for (int i : columns) {
  59.                     String s = resultSet.getString(i);
  60.                     CharBuffer buffer = utf8charset.decode(windows1258charset.encode(s));
  61.                     System.out.println(String.format("%s = %s",s, buffer));
  62.                     resultSet.updateString(i,new String(buffer.array()));
  63.                 }
  64.                 resultSet.updateRow();
  65.             } catch (SQLException e) {
  66.                 e.printStackTrace();
  67.                 return true;
  68.             }
  69.         }
  70. //            }
  71. //        }
  72.         return false;
  73.     }
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement