Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MySQL {
- private Connection connection;
- private String login = "login";
- private String password = "password"
- private String url = "jdbc:mysql://address:port/database";
- public Connection getConnection() throws SQLException {
- if (login != null)
- return DriverManager.getConnection(url, login, password);
- else return DriverManager.getConnection(url);
- }
- public void query(final String sql) { // выполнить запрос
- try {
- try {
- connection = getConnection();
- } catch (Exception e) {
- System.out.println("MySQL connection error");
- e.printStackTrace();
- }
- Statement statement = connection.createStatement();
- statement.executeUpdate(sql);
- statement.close();
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static String getString(final String query, final String columnLabel) { // вытащить данные
- String result = null;
- try {
- try {
- connection = getConnection();
- } catch (Exception e) {
- System.out.println("MySQL connection error");
- e.printStackTrace();
- }
- final Statement statement = connection.createStatement();
- final ResultSet resultSet = statement.executeQuery(query);
- if (resultSet.next()) {
- result = resultSet.getString(columnLabel);
- }
- statement.close();
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement