Guest User

Untitled

a guest
Sep 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import java.nio.charset.Charset;
  2. import java.sql.*;
  3.  
  4. public class App {
  5. private static String CONNECTION_STRING = "jdbc:mysql://192.168.99.10/?user=root&password=";
  6.  
  7. public static void main(String[] args) throws SQLException {
  8. setupDatabase();
  9. loadFromDatabase();
  10. }
  11.  
  12.  
  13. private static void loadFromDatabase() throws SQLException {
  14. try (Connection connection = getConnection(CONNECTION_STRING)){
  15. Statement statement = connection.createStatement();
  16. ResultSet resultSet = statement.executeQuery("SELECT * FROM test_latin.test_latin_table");
  17. while (resultSet.next()) {
  18. System.out.println(String.format("%s - %s", resultSet.getString(1), resultSet.getString(2)) );
  19. }
  20. }
  21. }
  22.  
  23. private static void setupDatabase() throws SQLException {
  24. try (Connection connection = getConnection(CONNECTION_STRING)){
  25. String value = new String("you’re".getBytes(), Charset.forName("Cp1252"));
  26.  
  27.  
  28. connection.setAutoCommit(true);
  29. String sqls[] = {
  30. "DROP DATABASE IF EXISTS test_latin",
  31. "CREATE DATABASE test_latin CHARACTER SET latin1",
  32. "USE test_latin",
  33. "CREATE TABLE test_latin_table (first_col TEXT, second_col CHAR(10))",
  34. String.format("INSERT INTO test_latin_table (first_col, second_col) VALUES ('%s', '%s')", value, value)
  35. };
  36. for (String sql: sqls) {
  37. Statement statement = connection.createStatement();
  38. statement.execute(sql);
  39. statement.close();
  40. }
  41. }
  42. }
  43.  
  44. private static Connection getConnection(String connectionString) throws SQLException {
  45. return DriverManager.getConnection(connectionString);
  46. }
  47. }
Add Comment
Please, Sign In to add comment