Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.charset.Charset;
- import java.sql.*;
- public class App {
- private static String CONNECTION_STRING = "jdbc:mysql://192.168.99.10/?user=root&password=";
- public static void main(String[] args) throws SQLException {
- setupDatabase();
- loadFromDatabase();
- }
- private static void loadFromDatabase() throws SQLException {
- try (Connection connection = getConnection(CONNECTION_STRING)){
- Statement statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery("SELECT * FROM test_latin.test_latin_table");
- while (resultSet.next()) {
- System.out.println(String.format("%s - %s", resultSet.getString(1), resultSet.getString(2)) );
- }
- }
- }
- private static void setupDatabase() throws SQLException {
- try (Connection connection = getConnection(CONNECTION_STRING)){
- String value = new String("youβre".getBytes(), Charset.forName("Cp1252"));
- connection.setAutoCommit(true);
- String sqls[] = {
- "DROP DATABASE IF EXISTS test_latin",
- "CREATE DATABASE test_latin CHARACTER SET latin1",
- "USE test_latin",
- "CREATE TABLE test_latin_table (first_col TEXT, second_col CHAR(10))",
- String.format("INSERT INTO test_latin_table (first_col, second_col) VALUES ('%s', '%s')", value, value)
- };
- for (String sql: sqls) {
- Statement statement = connection.createStatement();
- statement.execute(sql);
- statement.close();
- }
- }
- }
- private static Connection getConnection(String connectionString) throws SQLException {
- return DriverManager.getConnection(connectionString);
- }
- }
Add Comment
Please, Sign In to add comment