Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package bazadanych;
- import java.sql.*;
- import java.util.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Properties;
- public class BazaDanych {
- // init database constants
- private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DATABASE_URL = "jdbc:mysql://www.serwer1600151.home.pl/";
- private static final String USERNAME = "20748517_0000004";
- private static final String PASSWORD = "2&QPqua8rQDY";
- private static final String MAX_POOL = "250";
- // init connection object
- private Connection connection;
- // init properties object
- private Properties properties;
- // create properties
- private Properties getProperties() {
- if (properties == null) {
- properties = new Properties();
- properties.setProperty("user", USERNAME);
- properties.setProperty("password", PASSWORD);
- properties.setProperty("MaxPooledStatements", MAX_POOL);
- }
- return properties;
- }
- // connect database
- public Connection connect() {
- if (connection == null) {
- try {
- Class.forName(DATABASE_DRIVER);
- connection = DriverManager.getConnection(DATABASE_URL, getProperties());
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- return connection;
- }
- // disconnect database
- public void disconnect() {
- if (connection != null) {
- try {
- connection.close();
- connection = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String [ ] args){
- BazaDanych mysqlConnect = new BazaDanych();
- String sql = "INSERT INTO `20748517_0000004`.`user` (`Login`, `Password`) VALUES ('Andriu', 'asd')";
- try {
- PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql);
- statement.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- mysqlConnect.disconnect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement